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:
$('.WorkingHours').text()
won't contain any HTML (so nobr
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>
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).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
Find Out How Long an Ajax Request Took to Complete
How to Add/Subtract Dates with JavaScript
This' Does Not Work Properly in Another Event. I'm Clueless as to Why
JavaScript Detect an Ajax Event
In JavaScript, How to Check If an Array Has Duplicate Values
Access Denied to Jquery Script on Ie
Why Were Es5 Object Methods Not Added to Object.Prototype
How to Parse a Url Query Parameters, in JavaScript
A Proper Wrapper for Console.Log with Correct Line Number
How to Set the Prototype of a JavaScript Object That Has Already Been Instantiated
Convert Time Interval Given in Seconds into More Human Readable Form
Check If Jquery Has Been Loaded, Then Load It If False
How to Calculate the Latlng of a Point a Certain Distance Away from Another
Convert Js Object to Form Data