Mô tả
Sử dụng regex \b (word boundary) để khớp từ hoàn chỉnh, không khớp một phần của từ khác.
Ví dụ
var sentence = "This is the not javascript Tutorial"; var regularExpression = /\bjavascript\b/g; var output = regularExpression.test(sentence); console.log(output);
Kết quả
true
Giải thích
\b: ranh giới từ (giữa \w và \W, hoặc đầu/cuối chuỗi)g: global - tìm tất cả match- Khớp "javascript" nhưng KHÔNG khớp "javascripting", "postjavascript"
Các biến thể
// Case insensitive /\bjavascript\b/i // Tìm tất cả vị trí sentence.match(/\bjavascript\b/gi) // Thay thế từ chính xác sentence.replace(/\bjavascript\b/gi, "JS")