Solved

How to extract the number using regular expressions in JS?

Posted on 2011-02-22
5
342 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 75

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 75

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

867 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

14 Experts available now in Live!

Get 1:1 Help Now