Quantifiers
Quantifiers chỉ ra số lượng ký tự hoặc biểu thức cần khớp.
Try it
const ghostSpeak = "booh boooooooh";
const regexpSpooky = /bo{3,}h/;
console.log(ghostSpeak.match(regexpSpooky));
// Expected output: Array ["boooooooh"]
const modifiedQuote = "[He] ha[s] to go read this novel [Alice in Wonderland].";
const regexpModifications = /\[.*?\]/g;
console.log(modifiedQuote.match(regexpModifications));
// Expected output: Array ["[He]", "[s]", "[Alice in Wonderland]"]
const regexpTooGreedy = /\[.*\]/g;
console.log(modifiedQuote.match(regexpTooGreedy));
// Expected output: Array ["[He] ha[s] to go read this novel [Alice in Wonderland]"]
Các loại
Note: Trong phần sau, item không chỉ đề cập đến các ký tự đơn lẻ, mà còn bao gồm lớp ký tự và nhóm và backreference.
| Ký tự | Ý nghĩa |
|---|---|
x*
|
Khớp với item "x" đứng trước 0 hoặc nhiều lần. Ví dụ,
|
x+
|
Khớp với item "x" đứng trước 1 hoặc nhiều lần. Tương đương với
|
x?
|
Khớp với item "x" đứng trước 0 hoặc 1 lần. Ví dụ,
Nếu được dùng ngay sau bất kỳ quantifier nào trong số |
x{n}
|
Trong đó "n" là một số nguyên không âm, khớp chính xác "n" lần xuất hiện của
item "x" đứng trước. Ví dụ, |
x{n,}
|
Trong đó "n" là một số nguyên không âm, khớp ít nhất "n" lần xuất hiện của
item "x" đứng trước. Ví dụ, |
x{n,m}
|
Trong đó "n" và "m" là các số nguyên không âm và |
|
|
Mặc định các quantifier như
Lưu ý: Thêm |
Ví dụ
>Mẫu lặp lại
Trong ví dụ này, chúng ta khớp một hoặc nhiều ký tự từ với \w+, sau đó một hoặc nhiều ký tự "a" với a+, và cuối cùng kết thúc tại một ranh giới từ với \b.
const wordEndingWithAs = /\w+a+\b/;
const delicateMessage = "This is Spartaaaaaaa";
console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
Đếm ký tự
Trong ví dụ này, chúng ta khớp các từ có một chữ cái, các từ có từ 2 đến 6 chữ cái và các từ có từ 13 chữ cái trở lên.
const singleLetterWord = /\b\w\b/g;
const notSoLongWord = /\b\w{2,6}\b/g;
const longWord = /\b\w{13,}\b/g;
const sentence = "Why do I have to learn multiplication table?";
console.table(sentence.match(singleLetterWord)); // ["I"]
console.table(sentence.match(notSoLongWord)); // [ "Why", "do", "have", "to", "learn", "table" ]
console.table(sentence.match(longWord)); // ["multiplication"]
Ký tự tùy chọn
Trong ví dụ này, chúng ta khớp các từ kết thúc bằng "our" hoặc "or".
const britishText = "He asked his neighbour a favour.";
const americanText = "He asked his neighbor a favor.";
const regexpEnding = /\w+ou?r/g;
// \w+ One or several letters
// o followed by an "o",
// u? optionally followed by a "u"
// r followed by an "r"
console.table(britishText.match(regexpEnding));
// ["neighbour", "favour"]
console.table(americanText.match(regexpEnding));
// ["neighbor", "favor"]
Greedy và non-greedy
Trong ví dụ này, chúng ta khớp một hoặc nhiều ký tự từ hoặc dấu cách với [\w ]+ và [\w ]+?. Cái đầu tiên là greedy và cái thứ hai là non-greedy. Lưu ý cách cái thứ hai dừng lại ngay khi đáp ứng yêu cầu tối thiểu.
const text = "I must be getting somewhere near the center of the earth.";
const greedyRegexp = /[\w ]+/;
console.log(text.match(greedyRegexp)[0]);
// "I must be getting somewhere near the center of the earth"
// almost all of the text matches (leaves out the dot character)
const nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
console.log(text.match(nonGreedyRegexp));
// "I"
// The match is the smallest one possible
Xem thêm
- Hướng dẫn Biểu thức chính quy
- Hướng dẫn Lớp ký tự
- Hướng dẫn Assertions
- Hướng dẫn Nhóm và backreference
RegExp- Tài liệu tham khảo Biểu thức chính quy
- Quantifier:
*,+,?,{n},{n,},{n,m}