Character Sets are a set of characters that are written in square brackets[ ]. Which means finding a string based on the characters written between these square brackets[ ].

RegEx (Regular Expression) Character Sets In JavaScript - 3

RegEx Character Sets

In RegEx, we can create a set of characters in many ways. For this, see these examples:

Example 1 : In this example, we have set some characters between c and d. If any of these characters appear between c and d in the string, the string will match or else string will not match.

let regex = /c[opq]dexradar/; // can be an o,p,q

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



Example 2 : We can use multiple character sets in single regular expression.

let regex = /c[opq]dexrad[abc]r/; //can be an o,p,q + can be an a,b,c

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



Example 3 : If we have to match all lowercase and uppercase character from a to z then we use [a-z] and [A-Z]. If we want to match the digit, then we use the [0-9] character set. We can also use them together like: [a-zA-Z0-9]

let regex = /C[a-z]dex[A-Z]adar[0-9]/; // a-z + A-Z + 0-9

let string = "CodexRadar8";

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



Example 4 : In the following example, we use [a-zA-Z0-9] at the end of the codexradar string.

let regex = /CodexRadar[a-zA-Z0-9]/; // any character or digit come after the string

let string = "CodexRadarA8Z";

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



Example 5 :If we want to match all the characters in a string except a few characters, we use the Caret(^) Symbol for this. If we do not want the o,p,q character between c and d, then for this we will write the expression as follows: [^opq]

let regex = /C[^opq]dexRadar/; // can not be an o,p,q

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

Output : The string "CodexRadar" does not match the expression "C[^opq]dexRadar"


Note : With square brackets, we also set character ranges like : [a-f], [a-d], [0-5] etc.

Summary

In this post, we learned about character sets 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