Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of \W metacharacters in regular expressions (with code)

Detailed explanation of the use of \W metacharacters in regular expressions (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 13:30:044465browse

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

正则表达式\W元字符:
\W(大写)用来匹配非单词字符,它等价于"[^a-zA-Z0-9_]"。

语法结构:

构造函数方式:

new RegExp("\\W")

对象直接量方式:

/\W/

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

实例代码:

实例一:

var str="antzone<12>love"; 
var reg=new RegExp("\\W","g"); 
console.log(str.match(reg))

以上代码可以匹配除单词字符之外的所有字符。

实例二:

var str="antzone<12>love"; 
var reg=/\W/g; 
console.log(str.match(reg));

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

推荐阅读:

正则的三种模式(贪婪型、勉强型、占有型)详解

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

The above is the detailed content of Detailed explanation of the use of \W metacharacters in regular expressions (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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