Does \w also match numbers?

后端 未结 3 1526
失恋的感觉
失恋的感觉 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:09

    "\w" Matches any word character (alphanumeric & underscore). Only matches low-ascii characters (no accented or non-roman characters). Equivalent to [A-Za-z0-9_] Best source to learn : www.regexr.com

    0 讨论(0)
  • 2021-01-29 08:21

    Yes, \w does match latin numbers. \w == [A-Za-z0-9_]

    Assuming you want to remove @fooX, you can use:

    console.log("1 @bar @foo2 * 112".replace(/\s@\w+/g, ""));

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题