Bài viết được sự cho phép của tác giả Lưu Bình An
Giới thiệu
Regular Expression [hay gọi tắt là regex – đọc là ghi-ríc-cờ-sờ] là một string với format đặc biệt, với nó chúng ta có thểregex – đọc là ghi-ríc-cờ-sờ] là một string với format đặc biệt, với nó chúng ta có thể
- Tìm text trong string
- Thay thế substring trong string
- Trích xuất thông tin từ một string
Tất cả các ngôn ngữ đều có hỗ trợ regex. Có thể mỗi ngôn ngữ có đôi chổ khác nhau, tuy nhiên nhìn chung thì nó được áp dụng giống nhau cho hầu hết.
Regex có thể nói là rất khó học, khó viết, khó nhớ, khó sửa. Tuy nhiên có những việc chỉ có thể thực hiện được với regex.
Ví dụ, kiểm tra một
regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
4 có kết thúc bằng regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
5, regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
6 hay không"google.com" → true
"www.vietnam.football" → true
"google.foobar" → false
regex cho yêu cầu đó
/[\.com|\.football]$/i
Khai báo regex
Phân tách cái regex ở trên cái regex ở trên
7 đặt trước các ký tự đặc biệt, dấuregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
8 là một ký tự đặc biệtregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
9 là câu điều kiện ororregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
0 là điều kiện phải xuất hiện ở cuối string/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
- Đoạn nằm giữa
1, là chổ chúng ta viết các điều kiện. Đoạn-nằm-giữa có tên tiếng mỹ là pattern/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
- Chữ
2 ở cuối, phía sau/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
3 là một dạng setting,/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
2 là điều kiện chỉ hợp lệ nếu xuất hiện đúng một lần duy nhất. Có nhiều dạng setting khác nữa, phía dưới sẽ đề cập.setting,/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
2 là điều kiện chỉ hợp lệ nếu xuất hiện đúng một lần duy nhất. Có nhiều dạng setting khác nữa, phía dưới sẽ đề cập./^football/.test['football'] //✅ /^football/.test['bla football'] //❌
Trong javascript, regex là một object, có thể định nghĩa bằng 2 cách
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
Kiểm tra regex
Regex ở trên chúng ta đang tìm string
/^football/.test['football'] //✅
/^football/.test['bla football'] //❌
5, không giới hạn gì cả, chữ /^football/.test['football'] //✅
/^football/.test['bla football'] //❌
5 nằm ở đâu không quan trọng.Kiểm tra regex bằng
/^football/.test['football'] //✅
/^football/.test['bla football'] //❌
7, sẽ trả về giá trị /^football/.test['football'] //✅
/^football/.test['bla football'] //❌
8regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
Một số điều kiện có sẵn
Đã khó rồi, các bạn còn đề nghị một số kiểu viết tắt cho bắt anh em căng não ra học
9 tương tự như/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
0, chỉ là các giá trị số/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
1 tương tự như/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
2, không chưa các ký tự số/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
3 tương tự như/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
4, bao gồm ký tự chữ và số/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
5 tương tự như/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
6, không chứa ký tự chữ và số/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
7 chứa các ký tự khoản trống như: space, tab, newline/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
8 không chứa ký tự khoản trống/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
9 chứa ký tự null/football$/.test['football'] //✅ /football$/.test['bla football'] //✅ /football$/.test['football you'] //❌
0 chứa ký tự xuống dòng [newline]/^football$/.test['football'] //✅
1 chứ ký tự tab/^football$/.test['football'] //✅
Các điều kiện chúng ta hay sử dụng
Phải xuất hiện ở đầu câu:
2/^football$/.test['football'] //✅
Nếu muốn thêm điều kiện chữ
/^football/.test['football'] //✅
/^football/.test['bla football'] //❌
5 phải ở đầu câu đầu câu
/^football/.test['football'] //✅
/^football/.test['bla football'] //❌
Phải xuất hiện ở cuối câu:
0/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
Điều kiện chữ
/^football/.test['football'] //✅
/^football/.test['bla football'] //❌
5 nằm ở cuốicuối/football$/.test['football'] //✅
/football$/.test['bla football'] //✅
/football$/.test['football you'] //❌
Nếu kết hợp cả 2 cú pháp trên, ta có câu điều kiện chỉ được phép có đúng chữ
/^football/.test['football'] //✅
/^football/.test['bla football'] //❌
5/^football$/.test['football'] //✅
Phải bắt đầu bằng bằng 1 pattern và kết thúc bằng 1 pattern khác:
7/^football$/.test['football'] //✅
Bắt đầu bằng chữ
/^football$/.test['football'] //✅
8 và có kết thúc bằng chữ /^football$/.test['football'] //✅
9/^hey.*joe$/.test['hey joe'] //✅
/^hey.*joe$/.test['heyjoe'] //✅
/^hey.*joe$/.test['hey how are you joe'] //✅
/^hey.*joe$/.test['hey joe!'] //❌
Phải nằm trong khoản:
0/^hey.*joe$/.test['hey joe'] //✅ /^hey.*joe$/.test['heyjoe'] //✅ /^hey.*joe$/.test['hey how are you joe'] //✅ /^hey.*joe$/.test['hey joe!'] //❌
/[a-z]/ // có các ký tự a,b,c....x,y,z
/[A-Z]/ // có các ký tự A,B,C,...X,Y,Z
/[a-c]/ // có các ký tự a,b,c
/[0-9]/ // 0,1,2...8,9
/[a-z]/.test['a'] //✅
/[a-z]/.test['1'] //❌
/[a-z]/.test['A'] //❌
/[a-c]/.test['d'] //❌
/[a-c]/.test['dc'] //✅
Hợp thể
/[\.com|\.football]$/i
0/[\.com|\.football]$/i
1Chỉ được phép xuất hiện 1 lần: kết hợp
1,/^hey.*joe$/.test['hey joe'] //✅ /^hey.*joe$/.test['heyjoe'] //✅ /^hey.*joe$/.test['hey how are you joe'] //✅ /^hey.*joe$/.test['hey joe!'] //❌
2,/^football$/.test['football'] //✅
0/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
/[\.com|\.football]$/i
2Đảo ngược kết quả:
4/^hey.*joe$/.test['hey joe'] //✅ /^hey.*joe$/.test['heyjoe'] //✅ /^hey.*joe$/.test['hey how are you joe'] //✅ /^hey.*joe$/.test['hey joe!'] //❌
/[\.com|\.football]$/i
3Điều kiện hoặc:
9hoặc:regex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
9regex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
/[\.com|\.football]$/i
4Xuất hiện ít nhất một lần:
6/^hey.*joe$/.test['hey joe'] //✅ /^hey.*joe$/.test['heyjoe'] //✅ /^hey.*joe$/.test['hey how are you joe'] //✅ /^hey.*joe$/.test['hey joe!'] //❌
/[\.com|\.football]$/i
5Chỉ đúng hoặc sai, không có thể cả đúng và sai:
7/^hey.*joe$/.test['hey joe'] //✅ /^hey.*joe$/.test['heyjoe'] //✅ /^hey.*joe$/.test['hey how are you joe'] //✅ /^hey.*joe$/.test['hey joe!'] //❌
Nói vậy hơi khó hiểu, để giải thích rõ hơn nè, ví dụ yêu cầu phải là số từ 0-9, nếu kiểm tra
/^hey.*joe$/.test['hey joe'] //✅
/^hey.*joe$/.test['heyjoe'] //✅
/^hey.*joe$/.test['hey how are you joe'] //✅
/^hey.*joe$/.test['hey joe!'] //❌
8 => false vì nó có 1 là đúng, /^hey.*joe$/.test['hey joe'] //✅
/^hey.*joe$/.test['heyjoe'] //✅
/^hey.*joe$/.test['hey how are you joe'] //✅
/^hey.*joe$/.test['hey joe!'] //❌
9 là sai, còn /[a-z]/ // có các ký tự a,b,c....x,y,z
/[A-Z]/ // có các ký tự A,B,C,...X,Y,Z
/[a-c]/ // có các ký tự a,b,c
/[0-9]/ // 0,1,2...8,9
0 vì nó chỉ có sai không có đúng/[\.com|\.football]$/i
6Đúng n lần:
1/[a-z]/ // có các ký tự a,b,c....x,y,z /[A-Z]/ // có các ký tự A,B,C,...X,Y,Z /[a-c]/ // có các ký tự a,b,c /[0-9]/ // 0,1,2...8,9
/[\.com|\.football]$/i
7Đúng từ n đến m lần:
2/[a-z]/ // có các ký tự a,b,c....x,y,z /[A-Z]/ // có các ký tự A,B,C,...X,Y,Z /[a-c]/ // có các ký tự a,b,c /[0-9]/ // 0,1,2...8,9
/[\.com|\.football]$/i
8Nếu không đưa vào giá trị
/[a-z]/ // có các ký tự a,b,c....x,y,z
/[A-Z]/ // có các ký tự A,B,C,...X,Y,Z
/[a-c]/ // có các ký tự a,b,c
/[0-9]/ // 0,1,2...8,9
3 thì thõa từ /[a-z]/ // có các ký tự a,b,c....x,y,z
/[A-Z]/ // có các ký tự A,B,C,...X,Y,Z
/[a-c]/ // có các ký tự a,b,c
/[0-9]/ // 0,1,2...8,9
4 đến bao nhiêu cũng được/[\.com|\.football]$/i
9Điều kiện đó không bắt buộc:
5/[a-z]/ // có các ký tự a,b,c....x,y,z /[A-Z]/ // có các ký tự A,B,C,...X,Y,Z /[a-c]/ // có các ký tự a,b,c /[0-9]/ // 0,1,2...8,9
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
0Nhóm điều kiện
Để nhóm các điều kiện lại, đặt giữa
/[a-z]/ // có các ký tự a,b,c....x,y,z
/[A-Z]/ // có các ký tự A,B,C,...X,Y,Z
/[a-c]/ // có các ký tự a,b,c
/[0-9]/ // 0,1,2...8,9
6Ví dụ bên dưới, điều kiện thõa khi có đúng 3 ký tự số và theo sau đó là ký tự số hoặc chữ
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
1Nếu đặt dấu
/^hey.*joe$/.test['hey joe'] //✅
/^hey.*joe$/.test['heyjoe'] //✅
/^hey.*joe$/.test['hey how are you joe'] //✅
/^hey.*joe$/.test['hey joe!'] //❌
6 phía sau /[a-z]/ // có các ký tự a,b,c....x,y,z
/[A-Z]/ // có các ký tự A,B,C,...X,Y,Z
/[a-c]/ // có các ký tự a,b,c
/[0-9]/ // 0,1,2...8,9
6// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
2Lấy giá trị của 1 Group
Thay vì sử dụng
/[a-z]/ // có các ký tự a,b,c....x,y,z
/[A-Z]/ // có các ký tự A,B,C,...X,Y,Z
/[a-c]/ // có các ký tự a,b,c
/[0-9]/ // 0,1,2...8,9
9 để trả về boolean, sử dụng
0/[a-z]/.test['a'] //✅ /[a-z]/.test['1'] //❌ /[a-z]/.test['A'] //❌ /[a-c]/.test['d'] //❌ /[a-c]/.test['dc'] //✅
1/[a-z]/.test['a'] //✅ /[a-z]/.test['1'] //❌ /[a-z]/.test['A'] //❌ /[a-c]/.test['d'] //❌ /[a-c]/.test['dc'] //✅
Sẽ return một Array với những string thõa điều kiện
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
3Trường hợp group đó thõa điều kiện nhiều lần, chỉ trả về giá trị cuối cùng
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
4Setting
2: kiểm tra điều kiện nhiều lần/[a-z]/.test['a'] //✅ /[a-z]/.test['1'] //❌ /[a-z]/.test['A'] //❌ /[a-c]/.test['d'] //❌ /[a-c]/.test['dc'] //✅
2: không phân biệt hoa thường/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
3: cho phép kiểm tra xuống dòng/[a-z]/ // có các ký tự a,b,c....x,y,z /[A-Z]/ // có các ký tự A,B,C,...X,Y,Z /[a-c]/ // có các ký tự a,b,c /[0-9]/ // 0,1,2...8,9
5: bật hỗ trợ unicode/[a-z]/.test['a'] //✅ /[a-z]/.test['1'] //❌ /[a-z]/.test['A'] //❌ /[a-c]/.test['d'] //❌ /[a-c]/.test['dc'] //✅
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
5Thay thế chuỗi
Hàm
/[a-z]/.test['a'] //✅
/[a-z]/.test['1'] //❌
/[a-z]/.test['A'] //❌
/[a-c]/.test['d'] //❌
/[a-c]/.test['dc'] //✅
6 của String Object// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
6Có thể truyền vào tham số đầu là một Regex
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
7Thêm
/[a-z]/.test['a'] //✅
/[a-z]/.test['1'] //❌
/[a-z]/.test['A'] //❌
/[a-c]/.test['d'] //❌
/[a-c]/.test['dc'] //✅
2 để thay thế nhiều lần// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
8Sử dụng Group để di chuyển các phần text trong string
// tạo 1 object mới
const regex1 = new RegExp['football'];
// dùng regular expression literal
const regex2 = /football/
9Lấy số từ String
regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
0Kiểm tra email
regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
1Lấy đoạn test nằm giữa dấu /[a-z]/.test['a'] //✅
/[a-z]/.test['1'] //❌
/[a-z]/.test['A'] //❌
/[a-c]/.test['d'] //❌
/[a-c]/.test['dc'] //✅
8
/[a-z]/.test['a'] //✅
/[a-z]/.test['1'] //❌
/[a-z]/.test['A'] //❌
/[a-c]/.test['d'] //❌
/[a-c]/.test['dc'] //✅
regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
2Lấy nội dung ở giữa html tag
regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
3Bài viết gốc được đăng tải tại vuilaptrinh.com
Có thể bạn quan tâm:
- 20 công cụ và tài liệu hay ho dành cho Front-end
- Hướng dẫn code game Flappy Bird bằng JavaScript
- Hướng dẫn code chức năng đổi màu nền ngẫu nhiên với JavaScript
Xem thêm các vị trí tuyển Javascript hấp dẫn tại TopDev