How to Do String Replace in JavaScript to Convert '9.61' to '9:61'

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

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

How to use String.replace() to replace ALL occurrences of a pattern

Don't use replace, use split and join:

jqueryElements[index].className.split(' ').join('.');

How to replace characters using jquery

Here:

var rep = ['\'', '"', '!', '@', '#'];

for (var i = 0; i < rep.length; i++)
{
str = str.replace(rep[i], ""+(i+1));
}

Notice how the index of special characters in array rep is utilized while replacing.

Replacing text value from input field

You can use val() with callback and update based on old value

$("#myTextInput").val(function(index, oldVal) {  return oldVal.replace('hello', 'good morning');});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><input id="myTextInput" value="hello abc" />

remove all instances of a character in a string with something else javascript

There are three issues with your code:

  1. $('.WorkingHours').text() won't contain any HTML (so no br tags either), it only returns the text content of elements.

    $('.WorkingHours').text() returns:

    M,W,Th,F 7:30 AM - 4:00 PMTu 7:30 AM - 6:00 PM

    whereas $('.WorkingHours').html() returns:

    M,W,Th,F 7:30 AM - 4:00 PM<br>Tu 7:30 AM - 6:00 PM<br>

  2. You have to escape the inner backslash in your expression. Edit: Having a look at the output of .html() it actually does not contain <br /> but <br>. This might depend on the doctype of the document (not working example, working example).

  3. You have to assign the value back to the element.

You might be able to do

$('.WorkingHours').html(function(i, html) {
return html.replace(/<br\s*\/?>/g, " ");
});

but it would be much cleaner to not use regular expressions at all:

$('.WorkingHours').find('br').replaceWith(' ');

This finds all br element nodes and replaces them with a text node containing only a space.

DEMO

Update (in response to one of your comments): If you want to replace the last br with a full stop, you can use .last():

$('.WorkingHours')
.find('br').last().replaceWith('.')
.end().replaceWith(' ');

DEMO

How do I replace “br /br /” with “br /” in HTML using JavaScript and jQuery?

Use .html() to get and set the content passing it through the standard JS replace().

var div = $('#ValidationSummary1');
div.html(div.html().replace('<br /><br />', '<br />'));

Jquery - replace text in Val()

You can pass a function to .val() in 1.4+ like this:

$("#mySelector").val(function(i, v) { //index, current value
return v.replace("#","Custom Text");
});


Related Topics



Leave a reply



Submit