Solved

How to extract the number using regular expressions in JS?

Posted on 2011-02-22
5
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

631 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