JavaScript RegExp gModifier

RegExp 对象参考手册JavaScript RegExp Object

Definition and Usage

The g modifier is used to perform a global match (find all matches rather than stopping after the first match is found).

Syntax

new RegExp("regexp","g")

Or a simpler way:

/regexp/g


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the g modifier.


Examples

Example 1

Perform a global search for "is":

var str="Is this all there is?";
var patt1=/is/g;

The text belowis markedto show the positions where the expression gets matches:

Is this all there is?

Try it Yourself »

Example 2

Perform a global, case-insensitive search for "is":

var str="Is this all there is?";
var patt1=/is/gi;

The text belowis markedto show the positions where the expression gets matches:

Is this all there is?

Try it Yourself »


RegExp 对象参考手册JavaScript RegExp Object Other Extensions