gpt4 book ai didi

javascript - 正则表达式和逗号

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:29:49 31 4
gpt4 key购买 nike

您好,现在可以使用了,但我对原因感到困惑。

我正在学习正则表达式,需要从字符串中提取数字

'Discount 7.5%' should get 7.5
'Discount 15%' should get 15
'Discount 10%' should get 10
'Discount 5%' should get 5

等等

/\d,?.\d?/ //works

/\d,?.\d,?/ //doesn't works

/\d?.\d?/ //doesn't works

我认为后两个中的一个可以工作,有人可以解释一下吗。

最佳答案

使用易于理解的正则表达式快速而简单。

//Let the system to optimize the number parsing for efficiency
parseFloat(text.replace(/^\D+/, ""));

演示:http://jsfiddle.net/DerekL/4bnp8381/

关于javascript - 正则表达式和逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536775/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com