ailmcm
ailmcm

Reputation: 373

Regexp only one dot and digits

I faced some problems with regex; I want to leave only valid numbers in a string, people might enter:

  1. 11.2.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

Answers (3)

Toto
Toto

Reputation: 91518

How about:

/(?: |^)\d*\.?\d+(?: |$)/

this will match:

12
12.34
.34

Upvotes: 0

Norbert
Norbert

Reputation: 312

You must use quotes. Like this:

str.replace("/(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )/",'');

Upvotes: 0

Tushar Gupta
Tushar Gupta

Reputation: 15943

use regex as

\d*[0-9]\.\d*[0-9]

Upvotes: 1

Related Questions