Detailed explanation of the use of \d metacharacter in regular expressions (with code)

php中世界最好的语言
Release: 2018-03-30 11:46:08
Original
6989 people have browsed it

这次给大家带来正则表达式的\d元字符使用详解(附代码),使用正则表达式\d元字符的注意事项有哪些,下面就是实战案例,一起来看一下。

正则表达式\d元字符:
\d元字符可以匹配数字字符,等价于"[0-9]"。

语法结构:

构造函数方式:

new RegExp(\\d)

对象直接量方式:

/\d/

浏览器支持:

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

实例代码:

实例一:

var str="I love antzone \n and you 360"; var reg=new RegExp("\\d","g"); console.log(str.match(reg));
Copy after login

以上代码可以匹配字符串中的所有数字。

实例二:

var str="I love antzone \n and you 360"; var reg=/\d/g; console.log(str.match(reg));
Copy after login

此代码的作用和上面的代码是一样的。

还有很多情况下,例如很多情况下我们复制的代码最前面都有一个数字,如果上百行代码前面都有数字我们就可以通过正则来进行替换

例如:

1 alert("脚本之家"); 2 var str="I love antzone \n and you 360"; 3 var reg=/\d/g; 4console.log(str.match(reg));
Copy after login

例如下面的正则

^[0-9]*\.

就可以完美替换了前面的数字

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

推荐阅读:

js里的正则实现数字每隔四位用空格分隔效果

JS里怎么用正则将字符串中的多个空格替换为一个空格

在PHP里使用正则的效率 贪婪、非贪婪与回溯详解(附代码)


The above is the detailed content of Detailed explanation of the use of \d metacharacter in regular expressions (with code). 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
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!