0

I checked all the post and could not find correct solution so asking this question.

I have a string like " 3 rolls 7 buns 9 bars 7 cooks" and output that i am looking for is something like ["3 rolls","7 buns","9 bars","7 cooks"]

thanks.

2

2 Answers 2

2

You can use regular expression to achieve this,

var result = "3 rolls 7 buns 9 bars 7 cooks".split(/\s(?=\d)/);
conosole.log(result); //["3 rolls", "7 buns", "9 bars", "7 cooks"]

The regex concept used here is positive look ahead.

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

Comments

0

You can use String.prototype.match() with RegExp /\d+\s+\w+/g to match one or more digit characters followed by one or more space characters followed by one or more word characters

var str = "3 rolls 7 buns 9 bars 7 cooks";
var res = str.match(/\d+\s+\w+/g);
console.log(res);

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.