Reputation: 373
I faced some problems with regex; I want to leave only valid numbers in a string, people might enter:
11.2.2
abd11,asd11
and so on,
str = .replace(/[^[0-9]{1,2}([.][0-9]{1,2})?$]/g, '');
So I need to allow only digits and one dot from anything the user has entered;
This answer however doesn't work when I try to put it in:
str.replace(/(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )/,'');
It gives me a JS error. Please help me to understand what am I doing wrong.
p.s: thank You all guys for helping me, i found solution HERE, so i think thread may be closed.
Upvotes: 0
Views: 4299
Reputation: 312
You must use quotes. Like this:
str.replace("/(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )/",'');
Upvotes: 0