Cụm từ thông dụng [hay gọi tắt là regex – đọc là ghi-ríc-cờ-sờ] là một chuỗi có định dạng đặc biệt, với chúng ta có thể
- Tìm văn bản trong chuỗi
- Replace substring in string
- Trích xuất thông tin từ một chuỗi
Tất cả các ngôn ngữ đều có hỗ trợ regex. Có thể mỗi ngôn ngữ có các cặp đối lập nhau, tuy nhiên nhìn chung thì nó được áp dụng giống nhau cho hầu hết
Regex là gì?
12 Thư viện JavaScript trực quan hóa dữ liệu hot nhất năm 2022
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 cô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 for that request
/[\.com|\.football]$/i
Khai báo regex
Partitions regex at on
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 hoặcregex1.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 chuỗi/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
- Đoạn nằm giữa
1, là Họ ta viết các điều kiện. Đoạn-nằm- giữ lại có tên mỹ phẩm là mẫu/^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 định dạng cài đặt,/^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 định dạng cài đặt khác nữa, phía bên dưới sẽ đề cập đến/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
Trong javascript, regex là một đối tượng, có thể định nghĩa bằng 2 cách
regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
3Kiểm tra regex
Regex ở trên chúng ta đang tìm chuỗi
/^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ọngKiể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 đề xuất một số kiểu viết tắt để bắt anh em căng thẳng không học
9 tương tự như/^football/.test['football'] //✅ /^football/.test['bla football'] //❌
00, chỉ là các giá trị sốregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
01 tương tự nhưregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
02, không phải các ký tự sốregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
03 tương tự nhưregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
04, bao gồm các ký tự chữ và sốregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
05 tương tự nhưregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
06, không chứa các chữ cái và sốregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
- ______ 207 chứa các ký tự khoảng trống như. không gian, tab, dòng mới
08 không chứa các ký tự khoảng trốngregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
09 chứa ký tự nullregex1.test['football']; // => true regex1.test['blablabla football blablabla']; // => true regex2.test['footba']; // => false regex2.test['blab foot ballabla']; // => false
00 chứa ký tự xuống dòng [dòng mới]"google.com" → true "www.vietnam.football" → true "google.foobar" → false
01 chánh ký tự tab"google.com" → true "www.vietnam.football" → true "google.foobar" → false
Các điều kiện chúng ta hay sử dụng
must export at the first question.
02"google.com" → true "www.vietnam.football" → true "google.foobar" → false
Nếu muốn thêm điều kiện chữ
/^football/.test['football'] //✅
/^football/.test['bla football'] //❌
5 phải ở đầu câu________số 8Phả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ốiregex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
0Nế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"google.com" → true
"www.vietnam.football" → true
"google.foobar" → false
0Phải bắt đầu bằng 1 mẫu và kết thúc bằng 1 mẫu khác.
07"google.com" → true "www.vietnam.football" → true "google.foobar" → false
Bắt đầu bằng chữ
"google.com" → true
"www.vietnam.football" → true
"google.foobar" → false
08 và có kết thúc bằng chữ "google.com" → true
"www.vietnam.football" → true
"google.foobar" → false
09/[\.com|\.football]$/i
3must be in account.
30/[\.com|\.football]$/i
/[\.com|\.football]$/i
5/[\.com|\.football]$/i
6Hợp nhất
/[\.com|\.football]$/i
0/[\.com|\.football]$/i
1Chỉ được phép xuất hiện 1 lần. kết hợp ________ 431, ________ 302, ________ 80
/[\.com|\.football]$/i
2Đảo ngược kết quả.
34/[\.com|\.football]$/i
/[\.com|\.football]$/i
3Điều kiện hoặc.
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.
36/[\.com|\.football]$/i
/[\.com|\.football]$/i
5Chỉ đúng hoặc sai, không thể cả đúng và sai.
37/[\.com|\.football]$/i
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
/[\.com|\.football]$/i
38 => sai vì nó có 1 là đúng, /[\.com|\.football]$/i
39 là sai, còn /[\.com|\.football]$/i
50 vì nó chỉ có sai