JavaScript查找所有匹配项正则表达式|RegExp g 修饰符

JavaScript RegExp 对象


定义与用法

g 修饰符用于执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。


语法格式

new RegExp(” regexp”,”g”)

或者

/ regexp/g


浏览器支持

参数Google ChromeInternet ExplorerFirefoxSafariOpera
gYesYesYesYesYes

所有主要浏览器都支持 g 修饰符


示例代码

对 “is” 进行全局搜索:

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

下面被标记的文本显示了表达式获得匹配的位置:

Is this all there is?

亲自试试


更多示例

对 “is” 进行全局且大小写不敏感的搜索:

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

下面被标记的文本显示了表达式获得匹配的位置:

Is this all there is?

亲自试试


JavaScript RegExp 对象


相关