Reputation: 1545
I have this string:
string = 'addition and subtraction 1';
I want to split this string on spaces except when there's a number after a space. So like this:
['addition','and','subtraction 1']
How do I do this?
Upvotes: 1
Views: 590
Reputation: 2510
try this :
string = "addition and subtraction 1".split(/ (?!\d)/g));
output:
["addition", "and", "subtraction 1"]
Upvotes: 1
Reputation: 342
See This:
var str="addition and subtraction 1";
var splitstr=str.split(/ (?!\d)/g);
console.log(splitstr)
Upvotes: 2
Reputation: 3102
A negative lookahead in your split regex accomplishes this
console.log('addition and subtraction 1'.split(/ (?!\d)/g));
Upvotes: 5