BIỂU THỨC QUY TẮC TRONG
JAVASCRIPT
8/16/2012
1
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
Biểu thức quy tắc (regular expression)
Một biểu thức quy tắc là một kiểu được xác định trong
việc tìm kiếm tương ứng các ký tự kết hợp của một
chuỗi.
Các biểu thức quy tắc có thể dùng để tìm kiếm các mẫu
ký tự trong một chuỗi nhập vào từ người sử dụng.
Cú pháp khai báo: có 2 cách
var object= new RegExp(pattern, attributes);
var object= /pattern/attributes;
Trong đó:
Pattern: chuỗi mẫu cần tìm kiếm
Attributes: các thuộc tính tìm kiếm, gồm có g
(global-tìm tất cả các vị trí), i (không phân biệt
hoa thường), m (multiline).
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
2
Sử dụng biểu thức quy tắc
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
3
Biểu thức quy tắc có các phương thức sau:
test: kiểm tra pattern có xuất hiện hay không trả lại
kết quả true/false.
exec: trả lại giá trị pattern nếu tìm thấy pattern, ngược
lại trả lại giá trị null.
// cho ket qua false
</SCRIPT>
</HEAD>
</HTML>
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
5
Ví dụ Biểu thức quy tắc
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
6
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
re = /^T.+e$/;
source="Time and Tide wait for none";
str = re.exec (source); // tra lai ket qua la chuoi source da cho
window.alert(str);
re1=/T[a-z]+e/g;
str1=source.match(re1); // str1 la mang luu cac chuoi con tim thay
window.alert(str1.length); // do dai mang la 2
window.alert(str1) ; // tra lai ket qua la Time, Tide
</SCRIPT>
</HEAD>
<body>
</body>
</HTML>
Ví dụ Biểu thức quy tắc
8/16/2012
a special meaning:
MetacharacterDescription
. Find a single character, except newline or line
terminator
\w Find a word character
\W Find a non-word character
\d Find a digit
\D Find a non-digit character
\s Find a whitespace character
\S Find a non-whitespace character
\b Find a match at the beginning/end of a word
\B Find a match not at the beginning/end of a word
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
Metacharacters (Các ký tự quy ước đặc biệt)
\0 Find a NULL character
\n Find a new line character
\f Find a form feed character
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx Find the character specified by an octal
number xxx
\xdd Find the character specified by a
hexadecimal number dd
\uxxxx Find the Unicode character specified by a
hexadecimal number xxxx
10
8/16/2012
var reg=/^[-()0-9]+$/;
var reg1=/^[-()0-9]*$/;
Details:
^ match the start of the string
$ match the end of the string
[-()0-9] match any single character that is a hyphen, a
parenthesis, a space, or a digit.
+ match the longest available string consisting of one or
more of the preceding characters.
* match zero or more of the preceding characters.
12
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
Ví dụ về check email (1)
Example: Assume that a valid email address must have @
sign and there must be at least one dot”.” at the 4
th
or 3
th
position from the right of the string
var reg =/^.+?@.+?\ {2,3}$/;
^.+ Matches any string with contains at least one single
character except newline or line terminator at the
beginning of it.
^.+? Matches any string that contains zero or one
occurrences of ^.+
@.+? Do a global search for a “@", followed by at
least one single characters
\ +$ Matches any string with a “.”, followed by at least
string
.replace(
regexp/substr,newstring
)
Details:
regexp/substr: Required. A substring or a regular
expression.
Newstring: Required. The string to replace the found
value in parameter 1
Example:
<script type="text/javascript“>
var str="Visit Microsoft!";
document.write(str.replace(/microsoft/i, "W3Schools"));
</script>
15
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
Ví dụ: Check country code
Country code are two letters
var reg=/^[a-zA-Z][a-zA-Z]$/;
[a-zA-Z]: Find any character from uppercase A to
uppercase Z or find any character from lowercase a to
lowercase z 16
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
var arr = txt.split("/");
//document.write("<pre>");
for (i=0; i<arr.length;i++)
result.innerText += arr[i]+",";
18
8/16/2012
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT
Bài tập áp dụng
Xây dựng form đăng ký và viết lệnh javascript để bắt lỗi
form.
Trên form có các trường sau:
Tài khoản: Tài khoản có độ dài ít nhất 6 ký tự
Mật khẩu: Mật khẩu phải chứa cả chữ và số
Ngày tháng năm sinh: Ngày tháng năm sinh phải nhỏ hơn
ngày tháng năm hiện tại của hệ thống.
Email: Email phải đúng định dạng
Câu hỏi ngẫu nhiên và textbox ghi nhận câu trả lời của
người sử dụng.
Nút Đăng ký: Khi nhấn nút đăng ký thì kiểm tra các yêu cầu
trên. Nếu dữ liệu không hợp lệ thì yêu cầu nsd nhập lại. Nếu
dữ liệu đã hợp lệ thì thông báo đăng ký thành công
Nút Hủy bỏ 8/16/2012
19
ThS.Nguyễn Thị Quỳnh Như.Khoa CNTT