RegExp Characters [abc]
Example
A global search for the character "h" in a string:
let pattern = /[h]/g;
let result = text.match(pattern);
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:
Syntax
or simply:
/[abc]/
Syntax with modifiers
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 pattern = /[is]/gi;
A global search for the characters from lowercase "a" to lowercase "h":
let pattern = /[a-h]/g;
A global search for the character-from uppercase "A" to uppercase "E":
let pattern = /[A-E]/g;
A global search for characters from uppercase "A" to lowercase "e":
let pattern = /[A-e]/g;
A global, case-insensitive search for the character span [a-s]:
let pattern = /[a-s]/gi;
A "/g" and "/gi" search:
let result1 = text.match(/[THIS]/g);
let result2 = text.match(/[THIS]/gi);
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 |