Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中进行字符串替换以将“9.61”转换为“9:61”?(How do I do string replace in JavaScript to convert ‘9.61’ to ‘9:61’?)

Given the code line

(给定代码行)

var value = $("#text").val();

and value = 9.61 , I need to convert 9.61 to 9:61 .

(和value = 9.61 ,我需要将9.61转换为9:61 。)

How can I use the JavaScript replace function here?

(我如何在这里使用JavaScript替换功能?)

  ask by Nisanth Kumar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do it like this:

(像这样做:)

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);

Updated to reflect to current version of jQuery.

(更新以反映当前版本的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.

(作为开发人员,您需要知道哪个是哪个。)

Replace all occurrences(替换所有出现的事件)

To replace multiple characters at a time use some thing like this: name.replace(/&/g, "-") .

(要一次替换多个字符,请使用以下命令: name.replace(/&/g, "-") 。)

Here I am replacing all & chars with - .

(在这里,我用-替换所有&字符。)

g means "global"

(g表示“全球”)

Note - you may need to add square brackets to avoid an error - title.replace(/[+]/g, " ")

(注意 - 您可能需要添加方括号以避免错误 - title.replace(/[+]/g, " "))

credits vissu and Dante Cullari

(信用vissu和Dante Cullari)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...