Reputation: 230008
I know zero javascript, and have one simple task:
<img src="foo.com/$1.jpg"/>
, where $1 is the matched text (after spaces have been replaced).Can I bother you with a snippet that does this?
Upvotes: 0
Views: 193
Reputation: 140050
Assuming you already have the HTML source in a variable called html
, you can do something like this:
var converted = html.replace(/\[\[(.+?)\]\]/g, function (s, token) {
return '<img src="foo.com/'
+ token.split(" ").join("_")
+ '.jpg"/>';
});
To get or set the entire HTML inside <body>
, you can use document.body.innerHTML
. However, I would recommend specifically targeting elements of a certain id or class, if that string pattern doesn't really appear in random places in the page. I would also recommend that you use jQuery for locating these elements and changing their content:
$(".replacable").each(function () {
this.html(imagize(this.html()));
});
function imagize(html) {
return html.replace(/\[\[(.+?)\]\]/g, ...);
}
Upvotes: 2