Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
string.split(/ (?!\d)/g)
A negative lookahead in your split regex accomplishes this
console.log('addition and subtraction 1'.split(/ (?!\d)/g));
Add a comment
See This:
var str="addition and subtraction 1"; var splitstr=str.split(/ (?!\d)/g); console.log(splitstr)
try this :
string = "addition and subtraction 1".split(/ (?!\d)/g));
output:
["addition", "and", "subtraction 1"]
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
string.split(/ (?!\d)/g)