(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?

Issue

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

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

Answered By – Ian

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published