0

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.

1
  • have you tried id = parts[1];? Commented Aug 29, 2011 at 11:28

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

4 Comments

I have an another string like TestApplication Application 201.Then at that time i will face problem.Without array i need to retrieve the number.Is it possible to do that?
@shanuj Is TestApplication and Application always seperated by a space ??? if yes then try this var result=patt.exec(str.split(" ")[1]) and then pat_arra[0] will always have the last digit after the word Application.. edit the post as well...
If str = TestApplication100_101_102 Application200 then we will stuck with some errors i think.
@Kiran: Not if you will use the split(" ") method, you will always work the logic on the Application200 part....
0

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/

2 Comments

But what if var str = "TestApplication201 Application203";
@S.M.09, that was not a possibility put forth by the OP. Both examples the OP gave... TestApplication20 Application200 and TestApplication Application 201 have one three-digit number only.

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.