问题描述
给定代码行
var value = $("#text").val();
和 value = 9.61
,我需要将 9.61
转换为 9:61
。我如何在这里使用JavaScript替换功能?
and value = 9.61
, I need to convert 9.61
to 9:61
. How can I use the JavaScript replace function here?
推荐答案
这样做:
var value = $("#text").val(); // value = 9.61 use $("#text").text() if you are not on select box...
value = value.replace(".", ":"); // value = 9:61
// can then use it as
$("#anothertext").val(value);
更新以反映当前版本的jQuery。而且这里有很多答案最适合这种情况。作为开发人员,您需要知道哪个是哪个。
Updated to reflect to current version of jQuery. And also there are a lot of answers here that would best fit to any same situation as this. You, as a developer, need to know which is which.
替换多个字符一次使用这样的东西: name.replace(/& / g, - )
。在这里,我用 -
替换所有&
字符。 g
表示全球
To replace multiple characters at a time use some thing like this: name.replace(/&/g, "-")
. Here I am replacing all &
chars with -
. g
means "global"
注意 - 您可能需要添加方括号避免错误 - title.replace(/ [+] / g,)
Note - you may need to add square brackets to avoid an error - title.replace(/[+]/g, " ")
这篇关于如何在JavaScript中进行字符串替换以将“9.61”转换为“9:61”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!