Regular Expression: Any character that is NOT a letter or number

前端 未结 9 830
花落未央
花落未央 2020-12-07 19:32

I\'m trying to figure out the regular expression that will match any character that is not a letter or a number. So characters such as (,,@,£,() etc ...

Once found I

相关标签:
9条回答
  • 2020-12-07 20:02

    To match anything other than letter or number you could try this:

    [^a-zA-Z0-9]
    

    And to replace:

    var str = 'dfj,dsf7lfsd .sdklfj';
    str = str.replace(/[^A-Za-z0-9]/g, ' ');
    
    0 讨论(0)
  • 2020-12-07 20:04

    You are looking for:

    var yourVar = '1324567890abc§$)%';
    yourVar = yourVar.replace(/[^a-zA-Z0-9]/g, ' ');
    

    This replaces all non-alphanumeric characters with a space.

    The "g" on the end replaces all occurrences.

    Instead of specifying a-z (lowercase) and A-Z (uppercase) you can also use the in-case-sensitive option: /[^a-z0-9]/gi.

    0 讨论(0)
  • 2020-12-07 20:05
    • Match letters only /[A-Z]/ig
    • Match anything not letters /[^A-Z]/ig
    • Match number only /[0-9]/g or /\d+/g
    • Match anything not number /[^0-9]/g or /\D+/g
    • Match anything not number or letter /[^A-Z0-9]/ig

    There are other possible patterns

    0 讨论(0)
  • 2020-12-07 20:07

    try doing str.replace(/[^\w]/); It will replace all the non-alphabets and numbers from your string!

    Edit 1: str.replace(/[^\w]/g, ' ')

    0 讨论(0)
  • 2020-12-07 20:11

    This regular expression matches anything that isn't a letter, digit, or an underscore (_) character.

    \W
    

    For example in JavaScript:

    "(,,@,£,() asdf 345345".replace(/\W/g, ' '); // Output: "          asdf 345345"
    
    0 讨论(0)
  • 2020-12-07 20:13

    Have you tried str = str.replace(/\W|_/g,''); it will return a string without any character and you can specify if any especial character after the pipe bar | to catch them as well.

    var str = "1324567890abc§$)% John Doe #$@'.replace(/\W|_/g, ''); it will return str = 1324567890abcJohnDoe

    or look for digits and letters and replace them for empty string (""):

    var str = "1324567890abc§$)% John Doe #$@".replace(/\w|_/g, ''); it will return str = '§$)% #$@';

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