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.
它给了我一个JS错误。请帮我理解我做错了什么。
p.s: thank You all guys for helping me, i found solution HERE, so i think thread may be closed.
p.s:谢谢你们所有人帮助我,我在这里找到了解决方案,所以我认为线程可能会被关闭。
3 个解决方案
#1
1
use regex as
使用正则表达式
\d*[0-9]\.\d*[0-9]
#2
0
You must use quotes. Like this:
你必须使用引号。喜欢这个:
str.replace("/(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )/",'');
#3
0
How about:
/(?: |^)\d*\.?\d+(?: |$)/
this will match:
这将匹配:
12
12.34
.34
#1
1
use regex as
使用正则表达式
\d*[0-9]\.\d*[0-9]
#2
0
You must use quotes. Like this:
你必须使用引号。喜欢这个:
str.replace("/(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )/",'');
#3
0
How about:
/(?: |^)\d*\.?\d+(?: |$)/
this will match:
这将匹配:
12
12.34
.34