Regex extracting price or number value from string in JavaScript [duplicate]

You can use the following regex:


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) {
    document.getElementById("r2").innerHTML += m[0] + "<br/>";
<div id="r2"/>

Browse More Popular Posts

Leave a Comment