var _replacements = [];
var _AutoLink = function() {
$.each(_replacements, function(kk, vv) {
var stub = vv.phrase.replace(" ", "-")
var html = '$1'
if (vv.link.substring(0,4) !== "http") {
html = '$1'
}
vv.html = html
})
$('.ipsType_richText.ipsContained').html(function(k,v) {
var match, htmlfragments = [], htmlregex = /<[^>]*>/g, linkregex = /]*>.*?<\/a>/g;
var matches = []
// first strip links - keep them as-is
while (match = linkregex.exec(v)) {
htmlfragments.push(match);
}
var k = 0;
while (k < htmlfragments.length) {
var match = htmlfragments[k]
v = v.replace(match, ' [[[' + k + ']]] ');
k++
}
// then strip html tags
while (match = htmlregex.exec(v)) {
htmlfragments.push(match);
}
while (k < htmlfragments.length) {
var match = htmlfragments[k]
v = v.replace(match, ' [[[' + k + ']]] ');
k++
}
// add whitespace to help with matching whole words
v = " "+v+" "
// replace replacements globally and case-insensitive
// and preserve mentioned case with $1
$.each(_replacements, function(kk, vv) {
// match whole words
var repl = new RegExp('(\\s' + vv.phrase + '\\s)', 'i');
while (match = repl.exec(v)) {
// replace detected word (ignore spaces before/after)
var inner = new RegExp('(' + vv.phrase + ')', 'i');
while (match = inner.exec(v)) {
matches.push({
match: match,
html: vv.html.replace('$1', match[0])
});
v = v.replace(match[0], '{{{' + matches.length + '}}}')
break
}
break
}
});
// restore html tags
while (htmlfragments.length) {
var tag = ' [[[' + (htmlfragments.length-1) + ']]] '
v = v.replace(tag, htmlfragments.pop());
}
// restore tagged replacements
while (matches.length) {
var tag = '{{{' + matches.length + '}}}'
var match = matches.pop()
v = v.replace(tag, match.html);
}
return v;
});
};
$(window).load(function() {
_AutoLink();
});