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
nis the length (an integer), it won't have the.replacemethod. Did you meanS.replace(…)?Sand the expected output for that particular example.