How to extract the number using regular expressions in JS?

Posted on 2011-02-22
Last Modified: 2012-05-11
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?


Question by:Tolgar
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
LVL 20

Expert Comment

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" ) );

Author Comment

ID: 34952314
Why did you use replace?

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

LVL 20

Accepted Solution

Proculopsis earned 250 total points
ID: 34952581


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 );
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.
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


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 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)

738 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