We had a developer here who had added following line of code to a web application:
var amount = newValue.replace(/[^\d.-]/g, '');
The particular line deals with amount values that a user may enter into a field.
I know the following about the regular expression:
- that it replaces the matches with empty strings (i.e. removes them)
- that /g is a flag that means to match all occurrences inside "newValue"
- that the brackets [] denote a special group
- that ^ means beginning of the line
- that d means digits
Unfortunately I do not know enough to determine what kind of strings this should match. I checked with some web-based regex testers if it matches e.g. strings like 98.- and other alternatives with numbers but so far no luck.
My problem is that it seems to make IE very slow so I need to replace it with something else.
Any help on this would be appreciated.
Edit: Thanks to all who replied. I tried not just Google but sites like myregextester, regular-expressions.info, phpliveregex, and others. My problem was misunderstanding the meaning of ^ and expecting that this required a numeric string like 44.99.
We had a developer here who had added following line of code to a web application:
var amount = newValue.replace(/[^\d.-]/g, '');
The particular line deals with amount values that a user may enter into a field.
I know the following about the regular expression:
- that it replaces the matches with empty strings (i.e. removes them)
- that /g is a flag that means to match all occurrences inside "newValue"
- that the brackets [] denote a special group
- that ^ means beginning of the line
- that d means digits
Unfortunately I do not know enough to determine what kind of strings this should match. I checked with some web-based regex testers if it matches e.g. strings like 98.- and other alternatives with numbers but so far no luck.
My problem is that it seems to make IE very slow so I need to replace it with something else.
Any help on this would be appreciated.
Edit: Thanks to all who replied. I tried not just Google but sites like myregextester., regular-expressions.info, phpliveregex., and others. My problem was misunderstanding the meaning of ^ and expecting that this required a numeric string like 44.99.
Share Improve this question edited Oct 25, 2013 at 0:20 user100487 asked Oct 24, 2013 at 22:16 user100487user100487 2581 gold badge6 silver badges15 bronze badges 4-
1
Just try it... I don't see why you need to ask anyone. Pump some strings into the
.replace
and see what you get. Spoiler Alert it returns only digits, periods, and hyphens... You can even open a JS console in your browser and test by running something like:'abcABC123$%^.--_'.replace(/[^\d.-]/g, '');
– Jasper Commented Oct 24, 2013 at 22:20 - @SLaks: 2 is correct. No? – Blue Skies Commented Oct 24, 2013 at 22:23
- Might be helpful for future reference. Take a look at the description from here regex101./r/rV6mY0 – javaCity Commented Oct 24, 2013 at 22:45
- @BlueSkies: He changed the numbering (Or I made a typo). I meant what is now 3. – SLaks Commented Oct 25, 2013 at 0:14
2 Answers
Reset to default 13Inside the group, when the ^
is the first character, it works as a negation of the character matches. In other words, it's saying match any character that are not the ones in the group.
So this will mean "match anything that is not a digit, a period, or a hyphen".
The ^ character is a negation character.
var newValue = " x44x.-x ";
var amount = newValue.replace(/[^\d.-]/g, '');
console.log(amount);
will print
44.-
I suspect the developer maybe just wanted to remove trailing whitespaces? I would rather try to parse the string for numbers and remove anything else.