如何在 JavaScript 中进行字符串替换以将“9.61”转换为“9:61”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2145988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:57:14  来源:igfitidea点击:

How do I do string replace in JavaScript to convert ‘9.61’ to ‘9:61’?

javascriptjquery

提问by Nisanth Kumar

Given the code line

鉴于代码行

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

and value = 9.61, I need to convert 9.61to 9:61. How can I use the JavaScript replace function here?

并且value = 9.61,我需要转换9.619:61. 如何在此处使用 JavaScript 替换功能?

回答by Reigel

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. 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.

更新以反映当前版本的 jQuery。而且这里有很多答案最适合与此相同的任何情况。作为开发人员,您需要知道哪个是哪个。

Replace all occurrences

替换所有出现

To replace multiple characters at a time use some thing like this: name.replace(/&/g, "-"). Here I am replacing all &chars with -. gmeans "global"

要一次替换多个字符,使用这样一些东西:name.replace(/&/g, "-")。在这里,我将所有&字符替换为-. 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

回答by cletus

Probably the most elegant way of doing this is to do it in one step. See val().

做到这一点最优雅的方法可能是一步完成。见val()

$("#text").val(function(i, val) {
  return val.replace('.', ':');
});

compared to:

相比:

var val = $("#text").val();
$("#text").val(val.replace('.', ':'));

From the docs:

从文档:

.val( function(index, value) )

function(index, value)A function returning the value to set.

This method is typically used to set the values of form fields. For <select multiple="multiple">elements, multiple s can be selected by passing in an array.

The .val()method allows us to set the value by passing in a function. As of jQuery 1.4, the function is passed two arguments, the current element's index and its current value:

$('input:text.items').val(function(index, value) {
  return value + ' ' + this.className;
});

This example appends the string " items" to the text inputs' values.

.val( function(index, value) )

function(index, value) 返回要设置的值的函数。

此方法通常用于设置表单字段的值。对于 <select multiple="multiple">元素,可以通过传入一个数组来选择多个s。

.val()方法允许我们通过传入函数来设置值。从 jQuery 1.4 开始,该函数传递了两个参数,当前元素的索引及其当前值:

$('input:text.items').val(function(index, value) {
  return value + ' ' + this.className;
});

此示例将字符串“items”附加到文本输入的值。

This requires jQuery 1.4+.

这需要 jQuery 1.4+。

回答by Shadrack B. Orina

I love jQuery's method chaining. Simply do...

我喜欢jQuery的方法链。简单地做...

    var value = $("#text").val().replace('.',':');

    //Or if you want to return the value:
    return $("#text").val().replace('.',':');

回答by VIDesignz

A simple one liner:

一个简单的单衬:

$("#text").val( $("#text").val().replace(".", ":") );

回答by Anders

It can be done with the regular JavaScript function replace().

它可以通过常规的 JavaScript 函数来完成replace()

value.replace(".", ":");

回答by kravits88

You can use JavaScript functions like replace, and you can wrap the jQuery code in brackets:

你可以使用像 replace 这样的 JavaScript 函数,你可以将 jQuery 代码用括号括起来:

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

回答by andlrc

$("#text").val(function(i,v) { 
   return v.replace(".", ":"); 
});

回答by YOU

(9.61 + "").replace('.',':')

Or if your 9.61is already a string:

或者如果你9.61已经是一个字符串:

"9.61".replace('.',':')