-8

How to extract the value -1,234.23 from the following string using JavaScript regex?

"hello-sdvf-1,234.23 23 everybody 4"

should extract

-1,234.23

This regex returns null:

"hello-sdvf-1,234.23 23 everybody 4".match(/-?\d+\.\d+/);

and that one is OK for

"hello-sdvf-134.23 23 everybody 4".match(/-?\d+\.\d+/);

I need to extract numbers with commas, too.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Azad
  • 5,144
  • 4
  • 28
  • 56
  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](http://stackoverflow.com/questions/how-to-ask) – Cerbrus Jul 14 '15 at 06:44
  • 1
    What are the requirements? Will there be multiple values in one string? Should the number be negative? – Wiktor Stribiżew Jul 14 '15 at 06:50

4 Answers4

3

You can use the following regex:

-?\d+(?:,\d{3})*(?:\.\d+)?

It matches an optional hyphen (-?), then 1 or more digits (\d+), then optional 3-digit groups ((?:,\d{3})*), then an optional decimal part ((?:\.\d+)?).

var s = "hello-sdvf-1,234.23 23 everybody 4";
var res = s.match(/-?\d+(?:,\d{3})*(?:\.\d+)?/);
document.getElementById("r").innerHTML = res;
<div id="r"/>

Or, to match multiple values:

var re = /-?\d+(?:,\d{3})*(?:\.\d+)?/g; 
var str = 'hello-sdvf-1,234.23 23 everybody 4';
 
while ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    document.getElementById("r2").innerHTML += m[0] + "<br/>";
}
<div id="r2"/>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You can use following regex:

var str = "hello-sdvf-1,234.23 23 everybody 4";

str.match(/[+\-]?\d+(,\d+)?(\.\d+)?/)[0];


alert(str.match(/[+\-]?\d+(,\d+)?(\.\d+)?/)[0]);

Regex Explanation

  1. -?: Matches zero or one - literal
  2. 0-9: Matches any number
  3. ,: Matches ,
  4. .: Matches . literal
  5. +: Matches previous group one or more time
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

You can use this regex for matching:

var re = /\b[+-]?\d+(?:,\d{3})*(?:\.\d+)?\b/;

RegEx Demo

Code:

var m = "hello-sdvf-1,234.23 23 everybody 4".match(re);
//=> ["-1,234.23"]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Good question. I've come up with an initial function which does something slightly different. It will extract the numerical value from a string and convert it to a number type.

Because numbers are formatted differently in various locales, you can define which characters are expected to be used as thousands separators (default=","), and which are used for the decimal point (default=".").

    function extractNumber(value, thousands, decimals) {
        thousands = thousands || ',';
        decimals = decimals || '.';
        var regexp = new RegExp("[+-]?\\d+(?:["+thousands+"]\\d{3})*(?:["+decimals+"]\\d+)?", "g");
        value = value.match(regexp)[0];
        value = value.replace(new RegExp('['+thousands+']', 'g'), '');
        value = parseFloat(value);
        return value;
    };

Usage example:

extractNumber("hello-sdvf-1,234.23 23 everybody 4");

Returns

-1234.23 (a number, not a string)
kieranpotts
  • 1,510
  • 11
  • 8