Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

RegExp ? quantifier

JavaScript RegExp ? quantifier

RegExp Object Reference The JavaScript RegExp object


Definition and Usage

nThe ? quantifier matches any string containing zero or one n the specified string.

Syntax

new RegExp("

n

?")

Or a simpler way:

/

n

?/


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the ? quantifier.


Example

Example

Perform a global search for "1", including zero or one "0" immediately following it:

var str="1, 100 or 1000?";

var patt1=/10?/g;

The text marked below shows where the expression found matches:

1

,

10

0 or

10

00?

Try it »


RegExp Object Reference The JavaScript RegExp object