1

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?

1
  • 1
    string.split(/ (?!\d)/g) Commented May 22, 2017 at 13:12

3 Answers 3

5

A negative lookahead in your split regex accomplishes this

console.log('addition and subtraction 1'.split(/ (?!\d)/g));

Sign up to request clarification or add additional context in comments.

Comments

2

See This:

 var str="addition and subtraction 1";
    var splitstr=str.split(/ (?!\d)/g);
    console.log(splitstr)

Comments

1

try this :

string = "addition and subtraction 1".split(/ (?!\d)/g));

output:

["addition", "and", "subtraction 1"]

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.