Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string by integer in it using jquery

I have long string like

"When I was 10 year old"

I have to get this string split to an array: ["When I was", "10" , "year old"].

The integer can be different in different cases and the sentence may also change.

In short i want to split a string by integer in it Is it possible?

How can i use regex in conjugation with split in Jquery/java-script

like image 591
Kuttan Sujith Avatar asked Dec 16 '22 06:12

Kuttan Sujith


1 Answers

You can use

var str = "When I was 10 year old";
var arr = str.match(/(\D*)\s(\d*)\s(\D*)/).slice(1);

Result :

["When I was", "10", "year old"]
like image 171
Denys Séguret Avatar answered Dec 26 '22 07:12

Denys Séguret