Categories: Javascript对象tips

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

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

2 小时 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

7 天 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

2 周 ago

聊聊React和Vue组件更新的实现及区别

React 和 Vue 都是当…

3 周 ago