JavaScript对象:JavaScript RegExp g 修饰符

定义和用法

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

语法

new RegExp("regexp","g")

或者更简单方式:

/regexp/g

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

实例 1

对 “is” 进行全局搜索:

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

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

Is this all there is?
<script>
var str="Is this all there is?";
var patt1=/is/g;
document.write(str.match(patt1));
</script>

//result
is,is

实例 2

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

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

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

Is this all there is?
<script>
var str="Is this all there is?";
var patt1=/is/gi;
document.write(str.match(patt1));
</script>

//result
Is,is,is

作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/jsarrtips/2800.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年7月29日 下午2:23
下一篇 2021年7月29日 下午2:36

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注