REGEX Remove Space

前端 未结 5 1437
我寻月下人不归
我寻月下人不归 2020-12-20 07:44

I want to creating regex to remove some matching string, the string is phone number

Example user input phone number like this:

+jfalkjfkl saj f62 81 78

5条回答
  •  自闭症患者
    2020-12-20 07:59

    It would appear that you need two operations:

    1. Remove everything that is neither a blank nor a digit:

      s/[^ \d]//g;
      
    2. Remove all extra blanks:

      s/  +/ /g;
      

      If you need to remove leading and trailing blanks too:

      s/^ //;
      s/ $//;
      

      (after the replace multiple blanks with a single blank).

    You can use \s to represent more space-like characters than just a blank.

提交回复
热议问题