I forgot to include the initializing of the from/to arrays in the code. This is now fixed.
Implemented an acronym replacer in
JavaScript, that I have put in my blog form. Handy when I use lots of acronyms. I will extend it further to handle macro-style replacement. The snapshot of the code looks like this:
var fromAcronyms=new Array();
var toAcronyms=new Array();
function autoReplace(f) {
if (fromAcronyms.length != toAcronyms.length) {
alert('Acronym arrays differ');
return;
}
var tmp=f.value;
for (i=0; i < fromAcronyms.length; i++) {
tmp = tmp.replace(
new RegExp(' ' + fromAcronyms[i] + ' ', 'gi'),
' <acronym title="' + toAcronyms[i] + '">' +
fromAcronyms[i] + '</acronym> ');
}
f.value=tmp;
}
function addAcronym(from, to) {
fromAcronyms.length+=1;
toAcronyms.length+=1;
fromAcronyms[fromAcronyms.length-1]=from;
toAcronyms[toAcronyms.length-1]=to;
}
addAcronym('ACL', 'Access Control List');
addAcronym('CSS', 'Cascading Style Sheet');
addAcronym('DOM', 'Document Object Model');
addAcronym('HTML', 'Hyper Text Markup Language');
addAcronym('HTTP', 'Hyper Text Transfer Protocol');
addAcronym('J2EE', 'Java 2 Enterprise Edition');
addAcronym('J2ME', 'Java 2 Micro Edition');
addAcronym('J2SE', 'Java 2 Standard Edition');
addAcronym('JSP', 'Java Server Pages');
addAcronym('JSSE', 'Java Secure Socket Extension');
addAcronym('NSF', 'Notes Storage File');
addAcronym('NTF', 'Notes Template File');
addAcronym('RSS', 'RDF Site Summary');
addAcronym('RDF', 'Resource Description Framework');
addAcronym('SSL', 'Secure Socket Layer');
addAcronym('URL', 'Universal Resource Locator');
addAcronym('XML', 'Extensible Markaup Language');
The autoReplace function is run on the onchange event in my textarea, like this:
<textarea onchange="autoReplace(this);">
Mark Pilgrim has written the "
Defining acronyms" article that is worth reading.