RegExp Characters [abc]
Example
A global search for the character "h" in a string:
let text = "Is this all there is?";
let pattern = /[h]/g;
Try it Yourself »
Description
The /[abc]/
expression matches any the characters between the brackets.
RegExp Brackets
Brackets [] specifies matches for the characters inside the brackets.
Brackets can define single characters, groups, or character spans:
[a] | Matches the character a |
[abc] | Matches the characters a, b, or c |
[A-Z] | Matches all characters from uppercase A to uppercase Z |
[a-z] | Matches all characters from lowercase a to lowercase z |
[0-9] | Matches all digits from 0 to 9 |
Syntax
new RegExp("[abc]")
or simply:
/[abc]/
Syntax with modifiers
new RegExp("[abc]", "g")
or simply:
/[abc]/g
Tip
The [^abc] expression matches characters NOT between the brackets.
More Examples
A global search for the characters "i" and "s" in a string:
let text = "Do you know if this is all there is?";
let pattern = /[is]/gi;
Try it Yourself »
A global search for the characters from lowercase "a" to lowercase "h":
let text = "Is this all there is?";
let pattern = /[a-h]/g;
Try it Yourself »
A global search for the character-from uppercase "A" to uppercase "E":
let text = "I SCREAM FOR ICE CREAM!";
let pattern = /[A-E]/g;
Try it Yourself »
A global search for characters from uppercase "A" to lowercase "e":
let text = "I Scream For Ice Cream, is that OK?!";
let pattern = /[A-e]/g;
Try it Yourself »
A global, case-insensitive search for the character span [a-s]:
let text = "I Scream For Ice Cream, is that OK?!";
let pattern = /[a-s]/gi;
Try it Yourself »
A "/g" and "/gi" search:
let text = "THIS This this";
let result1 = text.match(/[THIS]/g);
let result2 = text.match(/[THIS]/gi);
Try it Yourself »
Regular Expression Methods
Regular Expression Search and Replace can be done with different methods.
These are the most common:
String Methods
Method | Description |
---|---|
match(regex) | Returns an Array of results |
matchAll(regex) | Returns an Iterator of results |
replace(regex) | Returns a new String |
replaceAll(regex) | Returns a new String |
search(regex) | Returns the index of the first match |
split(regex) | Returns an Array of results |
RegExp Methods
Method | Description |
---|---|
regex.exec() | Returns an Iterator of results |
regex.test() | Returns true or false |
Browser Support
/[abc]/
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |