Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx: returning the numbers from a string

Status
Not open for further replies.

fcoomermd

Programmer
Nov 20, 2002
218
CA
How can I do this,
I have a string like this:
var address="121Anywhere";
I want to seperate the 121 from the Anywhere to 2 different variables.
var a=121;
and
var b=Anywhere;

how with Regex?
Thanks
FC
 
FC,

Not sure about RegExp, but you can do it with parseInt:

Code:
alert(parseInt('121Anywhere'));

Will alert "121".

Hope this helps!

Dan
 
match(re); will return an array (if global) or the first match if not global

var address="121Anywhere";
alert(address.match( /\d+/g ));

var address="121Anywhere345";
alert(address.match( /\d+/g ));

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Code:
var address = "121Anywhere";
var re = /^(\d+)(.*)$/;
obj = re.exec(address);

where obj will be an array with:
Code:
obj[0] = "121Anywhere"
obj[1] = "121"
obj[2] = "Anywhere"
so you can set a and b as such:

Code:
var a = obj[1];
var b = obj[2];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top