Solved

How to extract the number using regular expressions in JS?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)
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…

778 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