JavaScript RegExp Group [abc]

Definition and Usage

The bracket [abc] specifies the matching item of the character within the brackets.

Square brackets can define a single character, a group, or a character range:

[abc] Any character a, b, or c.
[A-Z] Any character from uppercase A to uppercase Z.
[a-z] Any character from lowercase a to lowercase z.
[A-z] Any character from uppercase A to lowercase z.

Example

Global search for the character "h" in the string:

let text = "Is this all there is?";
let pattern = /[h]/g;

try it yourself

Hint

Please use [^abc] The expression finds any character not within parentheses.

Example 1

Perform a global search for the characters "i" and "s" in the string:

let text = "Do you know if this is all there is?";
let pattern = /[is]/gi;

try it yourself

Example 2

Global search for characters from lowercase "a" to lowercase "h" in the string:

let text = "Is this all there is?";
let pattern = /[a-h]/g;

try it yourself

Example 3

Global search for character range from uppercase "A" to uppercase "E":

let text = "I SCREAM FOR ICE CREAM!";
let pattern = /[A-E]/g;

try it yourself

Example 4

Global search for characters from uppercase "A" to lowercase "e": (It will search all uppercase letters, but only search lowercase letters from a to e.)

let text = "I Scream For Ice Cream, is that OK?!";
let pattern = /[A-e]/g;

try it yourself

Example 5

Global, case-insensitive search for character range [a-s]:

let text = "I Scream For Ice Cream, is that OK?!";
let pattern = /[a-s]/gi;

try it yourself

Example 6

Search for characters with "g" and "gi":

let text = "THIS This this";
let result1 = text.match(/[THIS]/g);
let result2 = text.match(/[THIS]/gi);

try it yourself

syntax

new RegExp("[abc]

or abbreviated as:

/[abc]/

Syntax with modifiers

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

or abbreviated as:

/[abc]/g

browser support

/[abc]/ is ECMAScript1 (ES1) feature.

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
supported supported supported supported supported supported

Regular expression search methods

In JavaScript, regular expression text search can be completed using different methods.

usagepattern (pattern)As regular expressions, these are the most commonly used methods:

example description
text.match(pattern) string method match()
text.search(pattern) string method search()
pattern.exec(text) RexExp method exec()
pattern.test(text) RexExp method test()