开发者

Issue with split string in JavaScript

I am trying to split string in JavaScript, bu开发者_C百科t 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.


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


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜