Search code examples
jqueryarraysregexsplitnumbers

Regex to split string into array where numeral exists followed by space, but skip over numerals that don't have a space after them


Consider the string below;

1 The dog is big 2 The dog is sitting on the sofa 3 The dog was born on the 4th of July 4 The dog is named Henry

I want to break this up where numerals exist followed by a space, gathering the number and everything after it up until the next numeral followed by a space. I'm using the regex \s*\d+\D+ which works, except when there's a numeral in the middle of the sentence NOT followed by a space (in this case, 4th). So, I want to get this:

["1 The dog is big","2 The dog is sitting on the sofa","3 The dog was born on the 4th of July","4 The dog is named Henry"]

but instead I get this:

["1 The dog is big","2 The dog is sitting on the sofa","3 The dog was born on the","4th of July","4 The dog is named Henry"]

How would I achieve this, bearing in mind that I'm hoping to avoid lookahead clauses because I'm using safari and they just don't work on safari?


Solution

  • You might be able to use the following match() approach:

    var input = "1 The dog is big 2 The dog is sitting on the sofa 3 The dog was born on the 4th of July 4 The dog is named Henry";
    var parts = input.match(/\b\d+(?: \w+)+?(?=\s+\d+\b|$)/gi);
    console.log(parts);

    The regex pattern used here matches each numbered item as:

    • \b\d+ match a leading number
    • (?: \w+)+? match one or more words, until
    • (?=\s+\d+\b|$) looking ahead and seeing the next nearest number OR the end of the input