0

I've been given a task that has to reformat incorrect phone numbers (that are strings) in javascript. The correct output has to be: 123-456-789 or 123-456-789-11 (depending on how many characters there are in the string)?

I have a basic java background and am still getting used to the way javascript works...

Example cases:

00-44 48 5555 8361 0 - 22 1985--324 555372654 

Example cases should ne:

004-448-555-583-61 022-198-53-24 555-372-654 

The code will eventually ignore the dashes and spaces so that correct output can be generated

This is what i have so far:

S = S.replace("", ""); for(var i = 0; i < n; i++) { var after = S.replace(/(\d{3})(?=\d)/g, '$1-'); console.log(after); } 

Cheers

9
  • If n is the length (an integer), it won't have the .replace method. Did you mean S.replace(…)? Commented May 19, 2018 at 13:45
  • Please add the example value(s) for S and the expected output for that particular example. Commented May 19, 2018 at 13:47
  • I didn't see that, thanks... But apparently the errors is coming from the if statement: ReferenceError: Invalid left-hand side in assignment) Commented May 19, 2018 at 13:47
  • test cases has been added Commented May 19, 2018 at 13:49
  • 1
    Oh, that as well. You should always include the error message in your question if you are getting one. Commented May 19, 2018 at 13:49

5 Answers 5

1

With S=["00-44 48 5555 8361","0 - 22 1985--324","555372654"]

All you need is one line:

S.map(str=>str.replace(/[- ]/g, "").match(/(\d{1,3})/g).join('-')); 

Which will iterate your array of strings, remove all spaces and -, then match 1 to 3 digits (greedy first, so 3 whenever possible) and ultimately join these matches with - between.

Run example below:

console.log( ["00-44 48 5555 8361","0 - 22 1985--324","555372654"] .map(str=>str.replace(/[- ]/g, '') .match(/(\d{1,3})/g) .join('-') ) );

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

Comments

1

One way to do it, is to first remove all the numbers then split on every third number. Followed by joining the array with a -

const tests = [ '123-15 68--5722', '123-15 68 52', '123--125--68--572', '123 a b c 15 68 d efg 52', ] function format(num) { // Remove everything that isn't a number return num.replace(/[^0-9]/g, '') // Spilt on every 3rd number .match(/\d{1,3}/g) // Join with dashes .join('-') } tests.forEach(num => console.log(format(num)))

Comments

0
  1. You used assignment operator(=) instead of equality operator(==) in the if block.

  2. As your code indicate 'n' is the length of the string. So n.replace() will not work.

Comments

0

Some general tips to solve this:

1) You might want to remove whitespaces and dashes from the input first, e.g.:

S = S.replace(" ", "") 

2) You can access a certain character in a string by index like this:

S[i] // e.g. "0" 

3) To check if you are at the third position were a dash needs to be added, just use modulo on the position:

if(n % 3 === 0) { /* add - */} 

4) You can build up a new string and add characters with +=:

let result = "123"; result += "-"; 

Comments

0

You can use a regular expression to match groups of three digits.

var inputs = ['00-44 48 5555 8361', '0 - 22 1985--324', '555372654', '123456798', '12345678911', '', '1']; inputs.forEach(function(input) { var strippedInput = input.replace(/[\s-]/g, ''); var formattedInput = strippedInput.replace(/(\d{3})(?=\d)/g, '$1-'); console.log(formattedInput); });

What the regex does is:

  • It looks for groups of exactly three digits (\d{3})
  • ...immediately followed by at least another digit (?=\d)
  • and replaces it with the matched group and a dash $1-

That strangely looking (?=\d) is a positive lookahead to not include the fourth digit after. This prevents adding a superfluous dash at the end of your formatted string.

Note that you can replace dashes and spaces in one single step using a character group [\s-].

Output:

004-448-555-583-61 022-198-532-4 555-372-654 123-456-798 123-456-789-11 1 

Comments