shanuj
shanuj

Reputation: 1

Issue with split string in JavaScript

I am trying to split string in JavaScript, but I am not successful.

JavaScript code:

var string = TestApplication20   Application200;
var parts = str.match(/(\d+)(\D.+)/).slice(1);
var id = parts[0];

I need to retrieve 200 from the string, but I am getting 20 as the result.

Please help me where I am doing wrong.

Upvotes: 0

Views: 104

Answers (2)

Jason Gennaro
Jason Gennaro

Reputation: 34855

If you are always looking for a three-digit number, you could do this

var str = "TestApplication20   Application200";
var parts = str.match(/\d{3}/);
alert(parts);

Working Example: http://jsfiddle.net/jasongennaro/PrFJv/

Upvotes: 0

Sangeet Menon
Sangeet Menon

Reputation: 9905

var str=  TestApplication20   Application200;
var str1=str.split(" ")[1];
var patt=/[0-9]+/g;
var pat_arra=new Array();
while (true) {
   var result=patt.exec(str);     //// or use var result=patt.exec(str1);
   if (result == null) break;
    pat_arra.push(result);

}
id=pat_arra[1]                    //// id=pat_arra[0] 

pat_arra[1] will have value 200   //// pat_arra[0] will have value 200

Upvotes: 1

Related Questions