Want to remove / ( -) . from phone number strings

I want to remove symbols in phone numbers. Sometimes it is in the format of 151-454-6545 but sometimes it is in (545)-(564)-(5465) and in sometimes it is in 548.445.8454. I am using

val.replace(/(\d{3})(\d{3})(\d{4})/, '($1) -$2-$3')

for replacing.. but it doesn't remove the dot.What to do remove the dot also? expected output like 545-455-4545

3 answers

  • answered 2020-10-01 06:19 Florian Bezagu

    I suggest to use a non-digit expression to replace them by '-' string :

      val.replace(/^\D+/, '')
      .replace(/\D+$/, '')
      .replace(/\D+/g, '-')
    

    Let me know if it does what you need.

    EDIT : trim whitespaces

  • answered 2020-10-01 06:41 Andre Elrico

    here is a version with only 1 regex

    https://regex101.com/r/Wavw45/1

    regex

    [^\d\n]*(\d{3})[^\d\n]+(\d{3,4})[^\d\n]+(\d{4})[^\d\n]*

    replace (or whatever pattern you want)

    ($1) -$2-$3

  • answered 2020-10-01 21:37 Ryszard Czech

    Use

    .replace(/^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*$/, '$1-$2-$3')
    

    See proof.

    Explanation

    --------------------------------------------------------------------------------
      ^                        the beginning of the string
    --------------------------------------------------------------------------------
      \D*                      non-digits (all but 0-9) (0 or more times
                               (matching the most amount possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        \d{3}                    digits (0-9) (3 times)
    --------------------------------------------------------------------------------
      )                        end of \1
    --------------------------------------------------------------------------------
      \D*                      non-digits (all but 0-9) (0 or more times
                               (matching the most amount possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \2:
    --------------------------------------------------------------------------------
        \d{3}                    digits (0-9) (3 times)
    --------------------------------------------------------------------------------
      )                        end of \2
    --------------------------------------------------------------------------------
      \D*                      non-digits (all but 0-9) (0 or more times
                               (matching the most amount possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \3:
    --------------------------------------------------------------------------------
        \d{4}                    digits (0-9) (4 times)
    --------------------------------------------------------------------------------
      )                        end of \3
    --------------------------------------------------------------------------------
      \D*                      non-digits (all but 0-9) (0 or more times
                               (matching the most amount possible))
    --------------------------------------------------------------------------------
      $                        before an optional \n, and the end of the
                               string