Open In App

JavaScript RegExp [abc] Expression

Last Updated : 28 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

  • [A-Z]: It is used to match any character from uppercase A to Z.
  • [a-z]: It is used to match any character from lowercase a to z.
  • [A-z]: It is used to match any character from uppercase A to lowercase z.
  • [abc…]: It is used to match any character between the brackets.
JavaScript
const regex = /[abc]/;
const str = "computer";

console.log(regex.test(str));

Output
true

Syntax

/[abc]/ 
// or
new RegExp("[abc]")

Syntax with modifiers

/\[abc]/g 
// or
new RegExp("[abc]", "g")

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

JavaScript
let str = 'GEEKSFORGEEKS: A computer science portal for geeks.';
let regex = /[A-G]/g;
let match = str.match(regex);

console.log(match);

Output
[
  'G', 'E', 'E',
  'F', 'G', 'E',
  'E', 'A'
]

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

JavaScript
let str = "GEEKSFORGEEKS: computer science portal for geeks.";
let regex = /[a-g]/g;
let match = str.match(regex);

console.log(match);

Output
[
  'c', 'e', 'c', 'e',
  'c', 'e', 'a', 'f',
  'g', 'e', 'e'
]

Supported Browsers

  • Chrome
  • Safari
  • Firefox
  • Opera
  • Edge

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.  


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: