Open In App

JavaScript RegExp [abc] Expression

The RegExp [abc] Expression in JavaScript is used to search any character between the brackets. The character inside the brackets can be a single character or a span of characters.

Syntax:



/[abc]/ 

or

new RegExp("[abc]")

Syntax with modifiers:



/\[abc]/g 

or

new RegExp("[abc]", "g")

Example 1: This example searches the characters between [A-G] i.e uppercase A to uppercase G in the whole string. 




function geek() {
    let str1 = 'GEEKSFORGEEKS is the computer'
        + ' science portal for geeks.';
    let regex4 = /[A-G]/g;
    let match4 = str1.match(regex4);
 
    console.log('Found ' + match4.length
        + ' matches: ' + match4);
}
 
geek();

Output
Found 7 matches: G,E,E,F,G,E,E

Example 2: This example searches the characters between [a-g] i.e lowercase a to lowercase g in the whole string. 




function geek() {
    let str1 = "GEEKSFORGEEKS is the computer"
        + " science portal for geeks.";
    let regex4 = /[a-g]/g;
    let match4 = str1.match(regex4);
 
    console.log("Found " + match4.length
        + " matches: " + match4)
 
}
geek();

Output
Found 12 matches: e,c,e,c,e,c,e,a,f,g,e,e

Supported Browsers: The browsers supported by RegExp [abc] Expression are listed below:

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  


Article Tags :