javascript - Return only numbers from string -
i have value in javascript as
var input = "rs. 6,67,000"
how can numerical values ?
result: 667000
current approach (not working)
var input = "rs. 6,67,000"; var res = str.replace("rs. ", "").replace(",",""); alert(res); result: 667,000
this great use regular expression.
var str = "rs. 6,67,000"; var res = str.replace(/\d/g, ""); alert(res); // 667000
\d
matches character not numerical digit. non digit replaced empty string. result digits in string.
the g
@ end of regular expression literal "global" meaning replaces matches, , not first.
this approach work variety of input formats, if "rs."
becomes else later, code won't break.
Comments
Post a Comment