'(javascript) If you had a string that was a word with a number at the end. How would you add a space between the word and the number?
For example:
let word = 'Winter4000'
const seperate = (word) => {
...
}
seperate(word) // output: Winter 4000
The word can be random and the number is always at the end.
Solution 1:[1]
let word = 'Winter4000'
const seperate = word.split(/([0-9]+)/).join(" ")
split it using regex pattern looking for numbers, then join it back together with a space added
Solution 2:[2]
const word = 'Winter4000';
const result = word.match(/[^\d]+/) + ' ' + word.match(/[\d]+/);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CollinD |
| Solution 2 | Alex Narbut |
