RegEx for maximum length with dot and replace function in JavaScript -
how can limit length of string replace function.
i need allow stings pattern: 11.11, 99.99, 100.00
1-3 numbers before dot, 1 dot , 2 numbers after dot.
i have this:
value = value.replace(/[^\d.]/, "");
using alternation |
operator test both sides of .
:
var str="11.11, 99.99, 100.00, 1333.05, 44.999" console.log(str.replace(/([, ]*\d{4,}\.\d*|\d*\.\d{3,})[, ]*/g, ""));
explanation:
[, ]*
: number of,
or space\d{4,}\.\d*
: 4 or more digits followed dot , number of digits(match numbers more 4 digits before dot)\d*\.\d{3,}
: number of digits followed dot , 3 or more digits(match numbers more 3 digits after dot)- if 1 of last 2 patterns found followed optional
,
or space, replace empty string""
Comments
Post a Comment