Does \w also match numbers?

后端 未结 3 1528
失恋的感觉
失恋的感觉 2021-01-29 07:33

I would expect that this

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

applied to this string

1 @bar @foo2 * 112
         


        
3条回答
  •  粉色の甜心
    2021-01-29 08:25

    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)

    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)

    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.

提交回复
热议问题