Print

JavaScript RegExp g 修饰符


作者: 文章来源:

本文原地址:https://www.mimiwuqi.com/webqianduan/197074.html

目录文章目录
  1. 定义和用法
  2. 语法
  3. 浏览器支持
  4. 实例 1
  5. 实例 2

定义和用法

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

语法

new RegExp("regexp","g")

或者更简单方式:

/regexp/g

浏览器支持

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

实例 1

对 “is” 进行全局搜索:

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

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

Is this all there is?

程序运行结果:

is,is

实例 2

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

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

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

Is this all there is?

程序运行结果:

Is,is,is

更多 建站教程 请访问 https://www.mimiwuqi.com/webqianduan/