JavaScript 中的全局 RegExp 属性的作用是什么?
htmljavascriptprogramming scripts更新于 2026/1/7 23:08:17
全局属性是 RegExp 对象的只读布尔属性。它指定特定正则表达式是否执行全局匹配,即它是否使用"g" 属性创建。
示例
您可以尝试运行以下代码以了解如何使用全局 RegExp 属性。
<html>
<head>
<title>JavaScript RegExp 全局属性</title>
</head>
<body>
<script>
var re = new RegExp( &"string"; );
if ( re.global ){
document.write(&"Test1 - 全局属性已设置&");
} else {
document.write(&"Test1 - 全局属性未设置&");
}
re = new RegExp( &"string", &"g" );
if ( re.global ){
document.write("<br />Test2 - 全局属性已设置");
} else {
document.write("<br />Test2 - 全局属性未设置");
}
</script>
</body>
</html>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

