我在替换'。'时遇到问题。从一个特定的变量,
var a =“ test.test 2.5测试”,
我的预期结果是“ test.test 25测试”,因此我要删除“。”从数字(2.5到25),但不能从任何字母(test.test必须保持为test.test)。用JavaScript可以做到这一点吗?
谢谢
最佳答案
编辑:更好:
"test.test 2.5 test".replace(/(\d)\.(\d)/, "$1$2");
这有效:
"test.test 2.5 test".replace(/(\d)\.(\d)/, function(a, b, c) { return b + c; });
这将
<digit> <dot> <digit>
替换为<digit> <digit>
。关于javascript - 仅替换字符串中的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14065686/