Hey! I have got a problem with some js regexp code. I thought Espresso is all about RegExp so this may be the right place to ask. If it’s not ok, please delete this topic.
My goal is: I want to highlight some words with JavaScript.
I’m using mootools.
function highlightWords() {
var searchFor = $$('.s').get('value'); // get search term √ works
var searchRegex = new RegExp("("+searchFor+")","gi"); // get regex √ works
var results = documentText.match(searchRegex); // match it √ works
for (i = 0; i < results.length; i++) {
// every single match √ works
// don't know what goes here, this might be useful.
}
// apply highlights
// CSS does the theming
newDocText = originalRendered.replace(searchRegex, "<span class='highlight'>"+searchFor+'</span>');
$$('.rendered').set('html', newDocText);
// STOP, here's the problem. When I search for “m”, it is not case sensitive.
// So every “M” gets a wrap but will be displayed as “m”.
// apply results
resultsText = (results.length == 1) ? " Result" : " Results";
$$('#results').set('text', results.length+resultsText);
}
The replacing replaces everything with either lowercase or uppercase terms. I want to search for ‘m’ and it replaces ‘m’ with ‘m’ and ‘M’ with ‘M’.
If anyone knows how to correct this, I would appreciate it.
Thanks!
