When we have to match a single word, then the word boundary proves to be of great use in it. This makes matching the word much easier than other methods of regex. We can also use it with digits.


RegEx (Regular Expression) Word Boundary and Assertions In JavaScript - 7


RegEx Word Boundary

RegEx Word Boundary can be understood by the following examples:


Example 1 : In this example "codex" word match with expression because "codex" is a single word. 


let regex = /\bcodex\b/;  //  word boundary

let string = "codex radar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex radar" matches the expression "\bcodex\b"


Example 2 : In this example we use the word boundary (\b) next to the word "codex". So our impression matches. If we use the word limit (\b) behind the word "codex" then the impression will also match.


let regex = /\bcodex/;  //  word boundary next to the word

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of "codexradar"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codexradar" matches the expression "\bcodex"


Example 3 : In this example "codex" word does not match with expression because "codex" is not  a single word. 


let regex = /\bcodex\b/;  //  word boundary

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // null

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codexradar" does not match the expression "\bcodex\b"


Example 4 : We can also use word boundary with digits.


let regex = /\b\d\d\b/;  //  word boundary with digits

let string = "1, 12, 123, 1234";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of "12"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "1, 12, 123, 1234" matches the expression "\b\d\d\b"


RegEx Assertions

RegEx Assertions can be understood by the following examples:


Example 1 : In this example, the expression matches the string that contains "o".


let regex = /C(?=o)/;  //  match string does contains "o"

let string = "Codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of "C"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "Codexradar" matches the expression "C(?=o)"


Example 2 : In this example, the expression matches the string that does not contain "o".


let regex = /C(?!o)/i;  //  match string does not contain "o"

let string = "Codexradar can change the world.";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of "c"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "Codexradar can change the world." matches the expression "C(?!o)"


Summary

In this post, we learned about regular expressions word boundary and asserions in JavaScript. Now you practice this. Keep visiting the blog to read more new similar posts.

Post a Comment

Previous Post Next Post