In this post, we will learn about metacharacter symbols of regular expressions in JavaScript. By using Metacharacter Symbols, we can improve the pattern of regular expression, which makes it much easier to search and replace a word in a string.


RegEx (Regular Expression) Metacharacter Symbols In JavaScript - 2


RegEx Metacharacter Symbols

We mainly use these Metacharacter Symbols in RegEx:


1. ^ (Caret Symbol)

The caret symbol is used when we want to find words starting with a particular letter in a string.


As in the example below we can see that in the given string we have to find string with the "c" letter:


let regex = /^c/; // Start with "c" letter

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result);

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

 

2. $ (Dollar Symbol)

The dollar symbol is used when we want to find words ending with a particular letter in a string.


As in the example below we can see that in the given string we have to find string ending with the "r" letter:


let regex = /r$/; // Ending with "r" letter

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result);

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


3. "."  (Dot Symbol)

Dot symbol is used when we have to match only one character.


In the following example, you can see that we have used the dot symbol between the letters c and d. Which means that only one character can come between c and d. If more than one character come between c and d, it will not be a match.


let regex = /c.dexradar/; // Only one character match

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result);

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


4. * (Asterisk Symbol)

Asterisk symbol is used when we have to match 0 and more characters.


This symbol is used to match such a string when either character does not appear between any two characters and or if one or more characters appear.


let regex = /c*dexradar/; // For 0 aur more characters match

let string = "cabcddexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result);

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


Note: If we want to match any special character, then we will use the backslash in front of that special character. Example of this given below:


let regex = /c\*dexradar/; // Use backslash for special character match

let string = "c*dexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result);

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


5. ? (Question mark Symbol)

The question mark symbol is used to match the optional character.


In the example below you can see that we have used this symbol between c and d. Which means that a character may or may not come between c and d.


let regex = /c?dexradar/; // Optional character match

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result);

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


Summary

In this post, we learned about metacharacter symbols of regular expressions in JavaScript. Now you practice this. Keep visiting the blog to read more new similar posts.

Post a Comment

Previous Post Next Post