ok aaron i changed my script, so now the function looks like this: function doInlineFormatting(content, ALLOW_URLS, ALLOW_HIDDEN_URLS) { // PARSE FOR INLINE FORMATTING var urls=[], str, formatter=function(s) {r return s .replace(/__(.*?)__/g,"<u>$1</u>") .replace(/\*\*(.*?)\*\*/g,"<b>$1</b>") .replace(/\/\/(.*?)\/\//g,"<i>$1</i>") .replace(/\^\^/g,"<br>") .replace(/\^\*/g,"<span style='margin-left: 1em;'></span>") .replace(/\$\$(#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}))\|(.*?)\$\$/g,"<span style='color: $1;'>$3</span>") .replace(/\]\_\[/g,"<hr style='border: 0; height: 0; border-top: 1px solid rgba(0, 0, 0, 0.1); border-bottom: 1px solid rgba(255, 255, 255, 0.3); margin-bottom: 2px; margin-top: 2px;'/>") .replace(/\~\L(.*?)\~\L/g,"<div style='text-align: left;'>$1</div>") .replace(/\~\C(.*?)\~\C/g,"<div style='text-align: center;'>$1</div>") .replace(/\~\R(.*?)\~\R/g,"<div style='float: right;'>$1</div><div style='clear: both;'></div>") ; }; str = _.reduce( content.match(/@@.*?@@/g), function (m,s,i) { var parts=s.replace(/@@(.*)@@/,'$1').split(/\|\|/), url = parts.shift().replace(/^\s*(http(s)?:\/\/|\/\/()|())/, 'http$2://'), text = formatter(parts.join('||')); if(ALLOW_URLS) { if(ALLOW_HIDDEN_URLS) { urls[i]='<a href="'+url+'">'+(text||url)+'</a>'; } else { urls[i]='<a href="'+url+'">'+text+' ['+url+']</a>'; } } else { urls[i]=s; } return m.replace(s,'@@'+i+'@@'); }, content ); str = _.reduce( content.match(/@[JHC]@.*?@[JHC]@/g), function(m, s, i) { var parts = s.replace(/@([JHC])@(.*)@[JHC]@/, '$1:$2').split(/:/), types = ('J' === parts[0] ? ['character','handout'] : ( 'H' === parts[0] ? ['handout'] : ['character']) ), matchName = parts[1].replace(/\s+/g,'').toLowerCase(); urls.push(_.chain(filterObjs(function(o){ return _.contains(types, o.get('type')) && (o.get('name').replace(/\s+/g,'').toLowerCase() === matchName); })) .map(function(o){ var n = o.get('name'), t = o.get('type'); return '<a title="'+t+': '+n+'" href=" <a href="http://journal.roll20.net/'+t+'/'+o.id+'">'+n+'</a" rel="nofollow">http://journal.roll20.net/'+t+'/'+o.id+'">'+n+'</a</a>> '; }) .value() .join(' ') || ' Error: Not found: "'+parts[1]+'" ' ); return m.replace(s, '@@' + (urls.length-1) + '@@'); }, str ); str=formatter(str); return _.reduce( urls, function(m,s,i) { return m.replace('@@'+i+'@@',s); }, str ); }