RegExp ? Quantifier, matches any string that contains zero or one occurrences - Javascript RegExp

Javascript examples for RegExp:Quantifier

Description

The n? quantifier matches any string that contains zero or one occurrences of n.

The following code shows how to Do a global search for a "1", followed by zero or one "0" characters:

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  w w  . ja  v  a 2s . com
    var str = "1, 100 or 1000?";
    var patt1 = /10?/g;
    var result = str.match(patt1);
    document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

Related Tutorials