Monday, July 9, 2012

jQuery Replace Strings

Below is simple jQuery code snippets to replace all the dots (.) present on HTML page and then replace it with hyphen.

$(document).ready(function() {
    var strNewString = $('body').html().replace(/\./g,'-');
    $('body').html(strNewString);
});
Remember: Replace functions returns a string which contains the updated text. So you need to first assign into a variable and then re-assign it.

Instead of, (.) or "-", text can be anything. All you need is to make change in the replace function arguments and you are good to go.
See live Demo and Code.
Feel free to contact me for any help related to jQuery, I will gladly help you.