The first replace finds the last 6 digits, with or without trailing separators.
The resulting match undergoes another replace call to substitute any digits for X's.
The first expression can also be relaxed to allow any separators: /(\d[^\d]?){6}$/, or even any number of separators: /(\d[^\d]*){6}$/.
If we want all the phone numbers to have the same format, we could use a regular expression to detect all the digits (assuming that the phone number is correct) and format it accordingly:
If we want to keep the original format (and again, assuming the phone number is correct), another solution would be transforming the string into an array (strings are immutable, an array would be easier to operate), parse the last 6 digits and return its concatenation:
Web and Software Frontend Developer. Used to work in Full-stack JavaScript.Been at this for a few years now, and think I now understand my specific domain pretty well. Still a lot to learn though.
Location
Manchester, UK
Education
Web Systems Design BSc at Sheffield Hallam University, Sheffield, UK
Web and Software Frontend Developer. Used to work in Full-stack JavaScript.Been at this for a few years now, and think I now understand my specific domain pretty well. Still a lot to learn though.
Location
Manchester, UK
Education
Web Systems Design BSc at Sheffield Hallam University, Sheffield, UK
JavaScript:
The first
replacefinds the last 6 digits, with or without trailing separators.The resulting match undergoes another
replacecall to substitute any digits for X's.The first expression can also be relaxed to allow any separators:
/(\d[^\d]?){6}$/, or even any number of separators:/(\d[^\d]*){6}$/.This is neat
Winner
JavaScript
If we want all the phone numbers to have the same format, we could use a regular expression to detect all the digits (assuming that the phone number is correct) and format it accordingly:
If we want to keep the original format (and again, assuming the phone number is correct), another solution would be transforming the string into an array (strings are immutable, an array would be easier to operate), parse the last 6 digits and return its concatenation:
Rust:
Javascript
This only works if you're expecting a certain value. Output would be weird otherwise.
Yeah, but the data validation is not required in this challange.
True, but it does expect the tests to all pass still, which they won't with that solution
Nice short solution!
If I'm being correct, for the string
"328 6587120", the expected output should be"328 6XXXXXX".But in your case, the output would be
"328 6XX-XXXX".ONE REGEX TO RULE THEM
Put your separators where you want! I'll find them all.
Dart - not a fan of being creative.
Haskell, similar idea to my solution in Ruby
Elm
Scala,
Ruby
JavaScript solution:
My basic solution in JS: Reverse string then replace with 'X' and count to 6
Didn't even know you can embed into comments, that's really cool.
Do you not feel that solution is overcomplicating it?