\w 是否也匹配数字? [英] Does \w also match numbers?

查看:34
本文介绍了\w 是否也匹配数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个

var r = new RegExp('\\s\\@[0-9a-z]+\\s', 'gi');

应用于这个字符串

1 @bar @foo2 * 112

会给我

1 * 112

相反,它导致

1 2 * 112

所以看起来 @[0-9a-z]+ 与数字不匹配.

so it looks like @[0-9a-z]+is not matching the number.

我肯定是弄错了,但我不知道是什么.

Surely I'm getting something wrong but I can't figure what.

这是在 javascript 中 - Firefox 49.0

This is in javascript - Firefox 49.0

推荐答案

我很快就设置好了它应该可以满足您的需求.

I quickly rigged this up which should do what you want.

var string = "1 @bar @foo2 * 112";

var matches = string.replace(/\s@\w+/gi,"");

console.log(matches)

我无法用那个正则表达式重现你的结果,但我确实发现最后的 \s 会停止@foo2 的匹配.

I could not reproduce your results with that regex, but I did find the final \s would stop the matching of @foo2.

var value = "1 @bar @foo2 * 112";
var matches = value.match(
     new RegExp("\\s\\@[0-9a-z]+\\s", "gi")
);
console.log(matches)

这是因为正则表达式无法匹配@foo 前面的空格,这是匹配的一部分.我希望这段代码可以解决您的问题.

This is because the regex can not then match the space in front of @foo which is required as part of the match. I hope this code solves your problem.

这篇关于\w 是否也匹配数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆