faqts : Computers : Programming : Languages : JavaScript : Language Core : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 15 people (80%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can extract the number for the following value(155) from (AOpen ATX Full Tower (13 Drive Bays) -$155.00)

Aug 25th, 2000 15:44
Martin Honnen, anthony saith,


JavaScript starting from 1.2 has regular expressions for such tasks:
var s = 'AOpen ATX Full Tower (13 Drive Bays) -$155.00';
var pattern = /\$(\d+(\.\d+)?)/
var match = pattern.exec(s);
var n = parseFloat(match[1]);
alert(n);
If you are not sure there is a currency value in the string change that 
code above to:
var s = 'AOpen ATX Full Tower (13 Drive Bays) -$155.00';
var pattern = /\$(\d+(\.\d+)?)/
var match = pattern.exec(s);
if (match) {
  var n = parseFloat(match[1]);
  alert(n);
}