Does something similar to gsub exist in javascript?
If others were looking for an equivalent to gsub in general, this only replaces the first match:
"aa".replace("a", "b") // "ba"
//g
replaces all matches:
"aa".replace(/a/g, "b") // "bb""aa".replace(new RegExp("a", "g"), "b"); // "bb"
You can access the raw HTML via a DOM element's innerHTML
property, or using JQuery's html
property wrapping it, and then perform the substitution:
var html = $(document).html();$(document).html(html.replace('{{title}}', 'I am a title');
EDIT:
As pointed out by Antti Haapala, replacing the entire document HTML can have side-effects you don't want to deal with, like scripts being reloaded. Thus, you should drill down to the most specific DOM element possible before performing the substitution, i.e.:
var element = $('#content');var html = element.html();element.html(html.replace('{{title}}', 'I am a title');
Well, you can use String.replace with a regex, but really, what you could use are jQuery Templates.