Home > Web Front-end > JS Tutorial > body text

How to use the {n,m} quantifier of regular expressions

php中世界最好的语言
Release: 2018-03-30 13:54:24
Original
3337 people have browsed it

这次给大家带来正则表达式的{n,m}量词如何使用,正则表达式{n,m}量词使用的注意事项有哪些,下面就是实战案例,一起来看一下。

正则表达式{n,m}量词:

{n,m}量词可以重复前面匹配的字符n-m次,至少n次,最多m次。

语法结构:
构造函数方式:

new RegExp("x{n,m}")
Copy after login

对象直接量方式:

/x{n,m}/
Copy after login

浏览器支持:
IE浏览器支持此元字符。
火狐浏览器支持此元字符。
谷歌浏览器支持此元字符。

实例代码:

实例一:

var str="100,1000 or 10000"; 
var reg=new RegExp("\\d{3,4}","g");
console.log(str.match(reg));
Copy after login

以上代码可以匹配3位或者4位数字。

实例二:

var str="100,1000 or 10000"; 
var reg=/\d{3,4}/g;
console.log(str.match(reg));
Copy after login

此代码的作用和上面的代码是一样的。只是正则的不同定义方式。

建议大家在学习的时候,通过这个JavaScript正则表达式在线测试工具方便查看效果。

看上面的效果三个数字只匹配1000,只匹配3-4个数字,少了多了都不行

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

正则的模式修饰符使用详解

正则表达式u修饰符的使用详解(附代码)

The above is the detailed content of How to use the {n,m} quantifier of regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!