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

JavascriptJquery

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

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"

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

> credits vissu and Dante Cullari

Solution 2 - Javascript

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

This requires jQuery 1.4+.

Solution 3 - Javascript

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

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

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

Solution 4 - Javascript

A simple one liner:

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

Solution 5 - Javascript

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

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

Solution 6 - Javascript

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

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

Solution 7 - Javascript

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

Solution 8 - Javascript

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

Or if your 9.61 is already a string:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNisanth KumarView Question on Stackoverflow
Solution 1 - JavascriptReigelView Answer on Stackoverflow
Solution 2 - JavascriptcletusView Answer on Stackoverflow
Solution 3 - JavascriptShadrack B. OrinaView Answer on Stackoverflow
Solution 4 - JavascriptVIDesignzView Answer on Stackoverflow
Solution 5 - JavascriptAndersView Answer on Stackoverflow
Solution 6 - Javascriptkravits88View Answer on Stackoverflow
Solution 7 - JavascriptAndreas LouvView Answer on Stackoverflow
Solution 8 - JavascriptYOUView Answer on Stackoverflow