Solved

How to extract the number using regular expressions in JS?

Posted on 2011-02-22
5
334 Views
Last Modified: 2012-05-11
Hi,
Can you please let me know how I can extract the "anynumber" from the following string?

<TD ALIGN=RIGHT NOWRAP><anystring HREF='/atv-bin/mytree.cgi?line=anystring&job=anynumber' TARGET=middle_frame

Open in new window


An example case:
The number that I want to assign to a variable using regular expressions is 515595:
<TD ALIGN=RIGHT NOWRAP><C HREF='/atv-bin/mytree.cgi?line=C&job=51595' TARGET=middle_frame

Open in new window


How can I do that?

Thanks,

0
Comment
Question by:Tolgar
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34952209

//try this:

var sample = "<TD ALIGN=RIGHT NOWRAP><C HREF='/atv-bin/mytree.cgi?line=C&job=51595' TARGET=middle_frame ";

alert( sample.replace( /^.*job=(\d+).*$/, "$1" ) );
0
 

Author Comment

by:Tolgar
ID: 34952314
Why did you use replace?

Can we use match as well? Because I don't want to replace it with something else.

Thanks,
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 250 total points
ID: 34952581

//or...

var sample = "<TD ALIGN=RIGHT NOWRAP><C HREF='/atv-bin/mytree.cgi?line=C&job=51595' TARGET=middle_frame ";
var digits = sample.replace( /^.*job=(\d+).*$/, "$1" );
alert( digits );

digits = sample.match( /(?:job=)(\d+)/ );
alert( (digits)? digits[1] : 0 );
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34953040
>>  Because I don't want to replace it with something else.

Well it wouldn't actually replace the string unless you assigned the result back to the source string. String replacment in JS is not in-place. This means when you call the replacement function, the resulting, replaced string is returned from the function and the original string is untouched. Many languages perform this kind of string handling.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34953060
As an example:
var source = "Hello World!"

alert(source);                   // displays: Hello World!
alert(source.replace(/l/, ''));  // displays: Heo Word!
alert(source);                   // displays: Hello World!
source = source.replace(/l/, ''));
alert(source);                   // displays: Heo Word!

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now