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

时间:2022-10-30 13:11:05

Given the code line

考虑到代码行

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

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

value = 9。61,我需要把9。61转换成9。61。如何使用JavaScript替换函数?

8 个解决方案

#1


536  

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 -. g means "global"

要一次替换多个字符,请使用如下内容:name。替换(/ & / g,“-”)。这里我用-替换所有的& chars。g的意思是“全球”

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

注意——您可能需要添加方括号以避免错误标题。替换(/[+]/ g," ")

credits vissu and Dante Cullari

这要归功于维斯苏和但丁·卡拉里

#2


67  

Probably the most elegant way of doing this is to do it in one step. See 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:

.val()方法允许我们通过传递函数来设置值。在jQuery 1.4中,函数传递两个参数:当前元素的索引和当前值:

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

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

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

This requires jQuery 1.4+.

这需要jQuery 1.4 +。

#3


41  

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('.',':');

#4


19  

A simple one liner:

一个简单的一个衬套:

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

#5


6  

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

可以使用常规的JavaScript函数replace()来完成。

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

#6


4  

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

可以使用JavaScript函数替换,也可以将jQuery代码用括号括起来:

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

#7


3  

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

#8


1  

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

Or if your 9.61 is already a string:

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

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

#1


536  

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 -. g means "global"

要一次替换多个字符,请使用如下内容:name。替换(/ & / g,“-”)。这里我用-替换所有的& chars。g的意思是“全球”

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

注意——您可能需要添加方括号以避免错误标题。替换(/[+]/ g," ")

credits vissu and Dante Cullari

这要归功于维斯苏和但丁·卡拉里

#2


67  

Probably the most elegant way of doing this is to do it in one step. See 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:

.val()方法允许我们通过传递函数来设置值。在jQuery 1.4中,函数传递两个参数:当前元素的索引和当前值:

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

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

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

This requires jQuery 1.4+.

这需要jQuery 1.4 +。

#3


41  

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('.',':');

#4


19  

A simple one liner:

一个简单的一个衬套:

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

#5


6  

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

可以使用常规的JavaScript函数replace()来完成。

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

#6


4  

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

可以使用JavaScript函数替换,也可以将jQuery代码用括号括起来:

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

#7


3  

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

#8


1  

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

Or if your 9.61 is already a string:

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

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