JavaScript对象:JavaScript RegExp [abc] 表达式

返回到:JavaScript对象:JavaScript RegExp 对象

定义和用法

[abc] 表达式用于查找方括号之间的任何字符。

方括号内的字符可以是任何字符或字符范围。

语法

new RegExp("[abc]")

或者更简单方式:

/[abc]/

所有主要浏览器都支持 [abc] 表达式

实例

在字符串中对字符范围 [a-h] 进行全局搜索:

var str="Is this all there is?";
var patt1=/[a-h]/g;

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

Is this all there is?

DEMO:

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

//result
h,a,h,e,e

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

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

相关推荐

发表回复

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