JavaScript RegExp Group [0-9]

Definition and Usage

[0-9] expression is used to find any character between the parentheses.

The numbers in the parentheses can be any digit from 0 to 9 or a range of digits.

Hint:Please use [^0-9] Expression finds any character that is not a number.

Instance

Example 1

Global search for the numbers 1, 2, 3, and 4 in the string:

let text = "123456789";
let pattern = /[1-4]/g;

Try it yourself

Example 2

Global search for the number "1" in the string:

let text = "12121212";
let pattern = /[1]/g;

Try it yourself

Syntax

new RegExp("[0-9]")

Or abbreviated as:

/[0-9]/

Syntax with modifiers

new RegExp("[0-9]", "g")

Or abbreviated as:

/[0-9]/g

Hint

Please use [^0-9] Expression finds any character that is not a number.

Browser support

/[0-9]/ It is an ECMAScript1 (ES1) feature.

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Supports Supports Supports Supports Supports Supports

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()