Chuỗi javascript thay thế mảng

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

  • regex1.test['football'];
    // => true
    regex1.test['blablabla football blablabla'];
    // => true
    regex2.test['footba'];
    // => false
    regex2.test['blab foot ballabla'];
    // => false
    7 đặt trước các ký tự đặc biệt, dấu 
    regex1.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ệt
  • regex1.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ặc
  • /^football/.test['football']     //✅
    /^football/.test['bla football'] //❌
    0 là điều kiện phải xuất hiện ở cuối chuỗi
  • Đoạn nằm giữa 
    /^football/.test['football']     //✅
    /^football/.test['bla football'] //❌
    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
  • Chữ 
    /^football/.test['football']     //✅
    /^football/.test['bla football'] //❌
    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

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
3

Kiể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ọ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'] //❌
8

regex1.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

  • /^football/.test['football']     //✅
    /^football/.test['bla football'] //❌
    9 tương tự như 
    regex1.test['football'];
    // => true
    regex1.test['blablabla football blablabla'];
    // => true
    regex2.test['footba'];
    // => false
    regex2.test['blab foot ballabla'];
    // => false
    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ố
  • ______ 207 chứa các ký tự khoảng trống như. không gian, tab, dòng mới
  • regex1.test['football'];
    // => true
    regex1.test['blablabla football blablabla'];
    // => true
    regex2.test['footba'];
    // => false
    regex2.test['blab foot ballabla'];
    // => false
    08 không chứa các ký tự khoảng trống
  • regex1.test['football'];
    // => true
    regex1.test['blablabla football blablabla'];
    // => true
    regex2.test['footba'];
    // => false
    regex2.test['blab foot ballabla'];
    // => false
    09 chứa ký tự null
  • "google.com" → true
    "www.vietnam.football" → true
    "google.foobar" → 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

Các điều kiện chúng ta hay sử dụng

must export at the first question.

"google.com" → true
"www.vietnam.football" → true
"google.foobar" → false
02

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ố 8

Phải xuất hiện ở cuối câu.

/^football/.test['football']     //✅
/^football/.test['bla football'] //❌
0

Điều kiện chữ 

/^football/.test['football']     //✅
/^football/.test['bla football'] //❌
5 nằm ở cuối

regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
0

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

"google.com" → true
"www.vietnam.football" → true
"google.foobar" → false
0

Phải bắt đầu bằng 1 mẫu và kết thúc bằng 1 mẫu khác.

"google.com" → true
"www.vietnam.football" → true
"google.foobar" → false
07

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
3

must be in account.

/[\.com|\.football]$/i
30

/[\.com|\.football]$/i
5
/[\.com|\.football]$/i
6

Hợp nhất

/[\.com|\.football]$/i
0
/[\.com|\.football]$/i
1

Chỉ đượ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ả.

/[\.com|\.football]$/i
34

/[\.com|\.football]$/i
3

Điều kiện hoặc.

regex1.test['football'];
// => true
regex1.test['blablabla football blablabla'];
// => true
regex2.test['footba'];
// => false
regex2.test['blab foot ballabla'];
// => false
9

/[\.com|\.football]$/i
4

Xuất hiện ít nhất một lần.

/[\.com|\.football]$/i
36

/[\.com|\.football]$/i
5

Chỉ đúng hoặc sai, không thể cả đúng và sai.

/[\.com|\.football]$/i
37

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

Chủ Đề