G
gregpinero
Hi guys,
What I'm trying to do is find all instances of an acronymn such as IBM
on a webpage and replace it with <acronym title="International Business
Machines">IBM</acronym>. However in my code below it replace the <,
and > with < and >.
Thus it replaces IBM with:
<acronym title="International Business
Machines">IBM</acronym>
at the HTML level.
Any help would be greatly appriciated.
-Greg
Here's the code I'm currently using:
(function() {
var replacements, regex, key, textnodes, node, s;
replacements = {
'IBM':'<acronym title="International Business
Machines">IBM</acronym>',
};
regex = {};
for (key in replacements) {
regex[key] = new RegExp(key, 'gi');
}
textnodes = document.evaluate( "//body//text()", document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < textnodes.snapshotLength; i++) {
node = textnodes.snapshotItem(i);
s = node.data;
for (key in replacements) {
s = s.replace(regex[key], replacements[key]);
}
node.data = s;
}
})();
What I'm trying to do is find all instances of an acronymn such as IBM
on a webpage and replace it with <acronym title="International Business
Machines">IBM</acronym>. However in my code below it replace the <,
and > with < and >.
Thus it replaces IBM with:
<acronym title="International Business
Machines">IBM</acronym>
at the HTML level.
Any help would be greatly appriciated.
-Greg
Here's the code I'm currently using:
(function() {
var replacements, regex, key, textnodes, node, s;
replacements = {
'IBM':'<acronym title="International Business
Machines">IBM</acronym>',
};
regex = {};
for (key in replacements) {
regex[key] = new RegExp(key, 'gi');
}
textnodes = document.evaluate( "//body//text()", document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < textnodes.snapshotLength; i++) {
node = textnodes.snapshotItem(i);
s = node.data;
for (key in replacements) {
s = s.replace(regex[key], replacements[key]);
}
node.data = s;
}
})();