Need a regex to remove everything except numbers

后端 未结 2 1958
傲寒
傲寒 2020-12-17 16:59

I need something besides [^0-9\\n], I want a regex(but dont know how to make one), that captures anything in a pattern of numbers like this, \"0000000000\" or \"000-000-0000

相关标签:
2条回答
  • 2020-12-17 17:30

    I always forget this one myself and go hunting the internet for the answer. Here it is, for my future reference, and the rest of the internet

    const rawNumber = '(555) 123-4567';
    const strippedNumber = rawNumber.replace(/\D+/g, '');
    

    Of course, my above example is JavaScript specific, but it can be adapted to other languages easily. The original post didn't specify language.

    0 讨论(0)
  • 2020-12-17 17:32

    You can search for all non-digits using:

    \D+
    

    OR

    [^0-9]+
    

    And replace by empty string.

    RegEx Demo

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