2

In a NodeJS application, I have a phone number field in my data that displays any number of phone numbers, one after another, in the same string:

\n\n \n (555) 555-5555 (Main)\n\n, \n\n \n (777) 777-777 (Domestic Fax)\n\n

I want to extract only the 'Main' phone number and convert it to the format 555-555-5555, that is whatever the Main number is. Basically I just want to extract whatever number precedes 'Main' and make it more legible (replacing the space with the '-')

I got as far as finding the correct regex string

const phoneRegex = /^[ \\n]*\(([0-9]{3})\)( )([0-9]{3}-[0-9]{4}) \(Main\).*$/

but when I try to replace the string, it doesn't work

foo.phone =foo.phone.replace(phoneRegex,'$1-$3')

I keep getting the entire matched portion -- that is all the \n's and everything up to and including '(Main)'

I have searched for how to replace the entire string with the capture groups, but I haven't figured it out.

6
  • 1
    Look beyond replace – you can extract a match first (see RegExp#exec or String#match), then create a new string from that. Commented May 21, 2020 at 9:57
  • 1
    Try s.replace(/^\s*\((\d{3})\)\s*(\d+)-(\d+)[^]*$/, '$1-$2-$3'), I believe the \n are the newline chars in your sample string (you must have copied/pasted the string literal value from the console rather than shared the literal text). Commented May 21, 2020 at 9:57
  • @WiktorStribiżew -- The \n characters actually come in the data returned -- I'm not sure how that would change the regex you provided Commented May 21, 2020 at 10:01
  • Change ^\s* with ^[^]*? Commented May 21, 2020 at 10:02
  • @WiktorStribiżew -- I'm afraid that didn't work. I keep getting the entire matched portion -- that is all the \n's and everything up to and including '(Main)' Commented May 21, 2020 at 10:07

2 Answers 2

1

You may use

var s = "\\n\\n  \\n  (555) 555-5555 (Main)\\n\\n, \\n\\n  \\n  (777) 777-777 (Domestic Fax)\\n\\n";
console.log(
  s.replace(/^[^]*?\((\d{3})\)\s*(\d+)-(\d+)\s*\(Main\)[^]*/, '$1-$2-$3')
);

Details

  • ^ - start of string
  • [^]*? - any 0 or more chars, as few as possible
  • \( - a (
  • (\d{3}) - Group 1: three digits
  • \) - a ) char
  • \s* - 0+ whitespaces
  • (\d+) - Group 2: one or more digits
  • - - a hyphen
  • (\d+) - Group 3: one or more digits
  • \s* - 0+ whitespaces
  • \(Main\) - a (Main) string
  • [^]* - the rest of the string.

See the regex demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! Indeed, I was using Regex101.com to build my original string. It was cruder and less efficient than yours, but it seemed to match. I wonder why it wasn't replacing the entire matched string correctly. In any case, I'll review your answer.
@Cerulean I still think your string contains newlines and not \ + n char combinations, and your \ns are actually "\n"s. . does not match newlines and carriage returns.
1

Would that work for you?

const str = `\n\n  \n  (555) 555-5555 (Main)\n\n, \n\n  \n  (777) 777-777 (Domestic Fax)\n\n`,

      [,code, phone] = str.match(/\((\d{3})\)\s(\d{3}\-\d{4})\s\(Main\)/),
      result = `${code}-${phone}`
      
console.log(result)

2 Comments

My bad -- I didn't want the 714 in the final result. (The 714 is from the actual data -- I was trying to make it more general with '555'). Basically I just want to extract whatever number precedes 'Main' and make it more legible (replacing the space with the '-')
@Cerulean : like that one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.