RegExp [0-9] Expression - Javascript RegExp

Javascript examples for RegExp:Bracket

Description

The [0-9] expression matches any character between the brackets.

The digits in the brackets can be any numbers or number range from 0 to 9.

[^0-9] expression matches any character that is NOT a digit.

Syntax

new RegExp("[0-9]")

or simply:

/[0-9]/

with modifiers

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

or simply:

/\[0-9]/g

The following code shows how to Do a global search for the numbers 1, 2, 3 and 4 in a string:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//w  ww .ja  va2 s. c  o m
    var str = "123456789";
    var patt1 = /[1-4]/g;
    var result = str.match(patt1);
    document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

Related Tutorials