当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 详解JavaScript正则表达式中的global属性的使用

详解JavaScript正则表达式中的global属性的使用

2019年08月02日  | 移动技术网IT编程  | 我要评论

 global是正则表达式对象的只读布尔属性。它指定是否一个特定的正则表达式进行全局匹配。否则它使用“g”属性创建。
语法

regexpobject.global

下面是参数的详细信息:

  •     na

返回值:

  • 如果“g”修改被设置返回“true”,否则返回“false”。

例子:

<html>
<head>
<title>javascript regexp global property</title>
</head>
<body>
<script type="text/javascript">
  var re = new regexp( "string" );

  if ( re.global ){
   document.write("test1 - global property is set"); 
  }else{
   document.write("test1 - global property is not set"); 
  }
  re = new regexp( "string", "g" );

  if ( re.global ){
   document.write("<br />test2 - global property is set"); 
  }else{
   document.write("<br />test2 - global property is not set"); 
  }
</script>
</body>
</html>

这将产生以下结果:

test1 - global property is not set
test2 - global property is set

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网