<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>Hi, I am writing a script that replaces text smilies with images using &lt;a href=&quot;http://userscripts.org/scripts/show/8400&quot;&gt;Smilize&lt;/a&gt;. It worked right out-of-the-box, but I edited it some (mostly regex) and now it's not working.

Can anyone see what's wrong with this script?
&lt;pre&gt;
&lt;code&gt;
function smilize() {
	try {
/*
	Regular Expressions to match all the smileys.
	// [\(\*\:;8][\w]*[dpo\)\(]?\*?(?![\w])(?![\/\/])
*/
		var smileyRegex = /((\*[\d\w]*\*)|[:;][\w()])/gi;
/*
	Regular Expression to match smiley if it is at the starting of line.
*/
		var smileyAtStartRegex = /^((((?:\*)[:;][\w()](?:\*)|[:;][\w()]))|(\*[\w]*\*))/i;
/*
	Regular Expressions to match individual smileys.
*/
		var smileysRegex = [ /:\)/ , /;\)/ , /:D/i , /:P/i , /:\(/ , /:o/i , /\*ill\*/i , /\*cool\*/i ,
 /\*yawn\*/i , /\*mad\*/i , /\*cry\*/i , /\*blush\*/i , /\*confused\*/i , /\*love\*/i ,
 /\*mean\*/i , /\*glad\*/i ];

/*
	DATA URIs for holding the smiley image data to be embedded into the page.
	Naming convention used is self explanatory.
	DATA URI generation courtesy - http://software.hixie.ch/utilities/cgi/data/data
*/
		var smileys = [ &quot;data:image/gif;base64,R0lGODlhEYYOhETY0gCIT49 ... (large array of data:uris)&quot; ];

/*
	XPath for getting all the text nodes in the html document containing the smileys.
*/
		var smileElements = document.evaluate(&quot;//text()[not(ancestor::script) &quot;+
								&quot;and not(ancestor::style) &quot; + 
								&quot;and not(ancestor::textarea) &quot; +
								&quot;and (contains(., ':)' ) &quot; +
								&quot;or contains(., ';)' ) &quot; +
								&quot;or contains(., ':D' ) &quot; +
								&quot;or contains(., ':d' ) &quot; +
								&quot;or contains(., ':P' ) &quot; +
								&quot;or contains(., ':p' ) &quot; +
								&quot;or contains(., ':(' ) &quot; +
								&quot;or contains(., ':o' ) &quot; +
								&quot;or contains(., ':O' ) &quot; +
								&quot;or contains(., '*cool*' ) &quot; +
								&quot;or contains(., '*yawn*' ) &quot; +
								&quot;or contains(., '*cry*' ) &quot; +
								&quot;or contains(., '*love*' ) &quot; +
								&quot;or contains(., '*confused*' ) &quot; +
								&quot;or contains(., '*mad*' ) &quot; +
								&quot;or contains(., '*ill*' ) &quot; +
								&quot;or contains(., '*glad*' ) &quot; +
								&quot;or contains(., '*blush*' ) &quot; +
								&quot;or contains(., '*mean*' ) ) ] &quot;,
							document,
							null,
							XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
							null);

/*
	Beginning traversing through the element nodes that the XPath expression returned.
	I choose to go from last node to first node so as to avoid any discrepancies since
	I will be adding nodes to the document and editing it's structure.
*/
		for(var i = smileElements.snapshotLength - 1;i &gt;= 0; i--) {	
			var elm = smileElements.snapshotItem(i);
			if(smileyRegex.test(elm.nodeValue)) {
				var elmSpan = document.createElement(&quot;span&quot;);
				elm.parentNode.replaceChild(elmSpan, elm);
				var text = elm.nodeValue;
/*
	Checking if smiley is at the starting of the line.
	If found replace it with appropriate smiley and then alter the 'text' of the element
	node cached to incorporate it having been replaced by an appropriate smiley.
*/
				if(smileyAtStartRegex.test(text)) {
					var smiley = smileyAtStartRegex.exec(text);
					text = text.substring(text.indexOf(smiley)+smiley.length+1);
					var smileyData=getSmiley(smileysRegex, smileys, smiley);
					var elmSmile = document.createElement(&quot;img&quot;);
					elmSmile.setAttribute(&quot;src&quot;, smileyData);
					elmSmile.setAttribute(&quot;align&quot;, &quot;center&quot;);
					elmSmile.style.border = &quot;none&quot;;
					elmSpan.appendChild(elmSmile);
				}
/*
	Setting the Smiley Regular Expression's lastIndex to 0 to start traversing through
	all the smileys that occur in the text node using 'exec' function.
*/
				smileyRegex.lastIndex = 0;
				for(var match = null, lastLastIndex = 0; (match = smileyRegex.exec(text)); ) {
					elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex, match.index)+RegExp.$1));
					var smiley = text.substring(match.index,smileyRegex.lastIndex);
					var smileyData=getSmiley(smileysRegex, smileys, smiley);
					var elmSmile = document.createElement(&quot;img&quot;);
					elmSmile.setAttribute(&quot;src&quot;, smileyData);
					elmSmile.setAttribute(&quot;align&quot;, &quot;center&quot;);
					elmSmile.style.border = &quot;none&quot;;
					elmSpan.appendChild(elmSmile);
					lastLastIndex = smileyRegex.lastIndex;
				}
				elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex)));
				elmSpan.normalize();
			}
/*
	Case when the smiley at the start is the only smiley in that particular node.
*/
			else if(smileyAtStartRegex.test(elm.nodeValue)) {
				GM_log(elm.nodeValue);
				var elmSpan = document.createElement(&quot;span&quot;);
				elm.parentNode.replaceChild(elmSpan, elm);
				var smiley = smileyAtStartRegex.exec(elm.nodeValue);
				var smileyData=getSmiley(smileysRegex, smileys, smiley);
				var elmSmile = document.createElement(&quot;img&quot;);
				elmSmile.setAttribute(&quot;src&quot;, smileyData);
				elmSmile.setAttribute(&quot;align&quot;, &quot;center&quot;);
				elmSmile.style.border = &quot;none&quot;;
				elmSpan.appendChild(elmSmile);
				elmSpan.appendChild(document.createTextNode(elm.nodeValue.substring(elm.nodeValue.indexOf(smiley)+smiley.length+1)));
				elmSpan.normalize();
			}
		}
	} catch (e) {
		GM_log(e);	//Catch and log any errors if encountered during the entire process.
	}
}

/*
	function to get the appropriate smiley from smileys array, by matching the smiley with
	each smileys regular expression.
*/
function getSmiley(smileysRegex, smileys, smiley) {
	for(var j=0;j&lt;smileys.length;j++) {
		if(smileysRegex[j].test(smiley)) {
			return smileys[j];
		}
	}
}

smilize();
&lt;/code&gt;
&lt;/pre&gt;</body>
  <body-html>&lt;p&gt;Hi, I am writing a script that replaces text smilies with images using &lt;a href=&quot;http://userscripts.org/scripts/show/8400&quot;&gt;Smilize&lt;/a&gt;. It worked right out-of-the-box, but I edited it some (mostly regex) and now it's not working.&lt;/p&gt;

&lt;p&gt;Can anyone see what's wrong with this script?
&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
function smilize() {
	try {
/*
	Regular Expressions to match all the smileys.
	// [\(\*\:;8][\w]*[dpo\)\(]?\*?(?![\w])(?![\/\/])
*/
		var smileyRegex = /((\*[\d\w]*\*)|[:;][\w()])/gi;
/*
	Regular Expression to match smiley if it is at the starting of line.
*/
		var smileyAtStartRegex = /^((((?:\*)[:;][\w()](?:\*)|[:;][\w()]))|(\*[\w]*\*))/i;
/*
	Regular Expressions to match individual smileys.
*/
		var smileysRegex = [ /:\)/ , /;\)/ , /:D/i , /:P/i , /:\(/ , /:o/i , /\*ill\*/i , /\*cool\*/i ,
 /\*yawn\*/i , /\*mad\*/i , /\*cry\*/i , /\*blush\*/i , /\*confused\*/i , /\*love\*/i ,
 /\*mean\*/i , /\*glad\*/i ];

/*
	DATA URIs for holding the smiley image data to be embedded into the page.
	Naming convention used is self explanatory.
	DATA URI generation courtesy - http://software.hixie.ch/utilities/cgi/data/data
*/
		var smileys = [ &quot;data:image/gif;base64,R0lGODlhEYYOhETY0gCIT49 ... (large array of data:uris)&quot; ];

/*
	XPath for getting all the text nodes in the html document containing the smileys.
*/
		var smileElements = document.evaluate(&quot;//text()[not(ancestor::script) &quot;+
								&quot;and not(ancestor::style) &quot; + 
								&quot;and not(ancestor::textarea) &quot; +
								&quot;and (contains(., ':)' ) &quot; +
								&quot;or contains(., ';)' ) &quot; +
								&quot;or contains(., ':D' ) &quot; +
								&quot;or contains(., ':d' ) &quot; +
								&quot;or contains(., ':P' ) &quot; +
								&quot;or contains(., ':p' ) &quot; +
								&quot;or contains(., ':(' ) &quot; +
								&quot;or contains(., ':o' ) &quot; +
								&quot;or contains(., ':O' ) &quot; +
								&quot;or contains(., '*cool*' ) &quot; +
								&quot;or contains(., '*yawn*' ) &quot; +
								&quot;or contains(., '*cry*' ) &quot; +
								&quot;or contains(., '*love*' ) &quot; +
								&quot;or contains(., '*confused*' ) &quot; +
								&quot;or contains(., '*mad*' ) &quot; +
								&quot;or contains(., '*ill*' ) &quot; +
								&quot;or contains(., '*glad*' ) &quot; +
								&quot;or contains(., '*blush*' ) &quot; +
								&quot;or contains(., '*mean*' ) ) ] &quot;,
							document,
							null,
							XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
							null);

/*
	Beginning traversing through the element nodes that the XPath expression returned.
	I choose to go from last node to first node so as to avoid any discrepancies since
	I will be adding nodes to the document and editing it's structure.
*/
		for(var i = smileElements.snapshotLength - 1;i &gt;= 0; i--) {	
			var elm = smileElements.snapshotItem(i);
			if(smileyRegex.test(elm.nodeValue)) {
				var elmSpan = document.createElement(&quot;span&quot;);
				elm.parentNode.replaceChild(elmSpan, elm);
				var text = elm.nodeValue;
/*
	Checking if smiley is at the starting of the line.
	If found replace it with appropriate smiley and then alter the 'text' of the element
	node cached to incorporate it having been replaced by an appropriate smiley.
*/
				if(smileyAtStartRegex.test(text)) {
					var smiley = smileyAtStartRegex.exec(text);
					text = text.substring(text.indexOf(smiley)+smiley.length+1);
					var smileyData=getSmiley(smileysRegex, smileys, smiley);
					var elmSmile = document.createElement(&quot;img&quot;);
					elmSmile.setAttribute(&quot;src&quot;, smileyData);
					elmSmile.setAttribute(&quot;align&quot;, &quot;center&quot;);
					elmSmile.style.border = &quot;none&quot;;
					elmSpan.appendChild(elmSmile);
				}
/*
	Setting the Smiley Regular Expression's lastIndex to 0 to start traversing through
	all the smileys that occur in the text node using 'exec' function.
*/
				smileyRegex.lastIndex = 0;
				for(var match = null, lastLastIndex = 0; (match = smileyRegex.exec(text)); ) {
					elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex, match.index)+RegExp.$1));
					var smiley = text.substring(match.index,smileyRegex.lastIndex);
					var smileyData=getSmiley(smileysRegex, smileys, smiley);
					var elmSmile = document.createElement(&quot;img&quot;);
					elmSmile.setAttribute(&quot;src&quot;, smileyData);
					elmSmile.setAttribute(&quot;align&quot;, &quot;center&quot;);
					elmSmile.style.border = &quot;none&quot;;
					elmSpan.appendChild(elmSmile);
					lastLastIndex = smileyRegex.lastIndex;
				}
				elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex)));
				elmSpan.normalize();
			}
/*
	Case when the smiley at the start is the only smiley in that particular node.
*/
			else if(smileyAtStartRegex.test(elm.nodeValue)) {
				GM_log(elm.nodeValue);
				var elmSpan = document.createElement(&quot;span&quot;);
				elm.parentNode.replaceChild(elmSpan, elm);
				var smiley = smileyAtStartRegex.exec(elm.nodeValue);
				var smileyData=getSmiley(smileysRegex, smileys, smiley);
				var elmSmile = document.createElement(&quot;img&quot;);
				elmSmile.setAttribute(&quot;src&quot;, smileyData);
				elmSmile.setAttribute(&quot;align&quot;, &quot;center&quot;);
				elmSmile.style.border = &quot;none&quot;;
				elmSpan.appendChild(elmSmile);
				elmSpan.appendChild(document.createTextNode(elm.nodeValue.substring(elm.nodeValue.indexOf(smiley)+smiley.length+1)));
				elmSpan.normalize();
			}
		}
	} catch (e) {
		GM_log(e);	//Catch and log any errors if encountered during the entire process.
	}
}

/*
	function to get the appropriate smiley from smileys array, by matching the smiley with
	each smileys regular expression.
*/
function getSmiley(smileysRegex, smileys, smiley) {
	for(var j=0;j&amp;lt;smileys&gt;&lt;/code&gt;
&lt;/pre&gt;&lt;/p&gt;</body-html>
  <created-at type="datetime">2008-07-22T00:45:44Z</created-at>
  <forumable-id type="integer">1</forumable-id>
  <forumable-type>Forum</forumable-type>
  <id type="integer">10784</id>
  <topic-id type="integer">2877</topic-id>
  <updated-at type="datetime">2008-07-22T00:48:14Z</updated-at>
  <user-agent nil="true"></user-agent>
  <user-id type="integer">39648</user-id>
</post>
