I use the search function for the documentation site, which when you select a search page displays a page with selected text (just like a PDF reader or netbeans does).
To achieve the highlight, I use javascript with:
function searchHighlight(searchTxt) {
var target = $('#page').html();
var re = new RegExp(searchTxt, 'gi');
target = target.replace(
re,
'<span class="high">' + searchTxt + '</span>'
);
$('#page').html(target);
}
Problem / Question:
Since the page contains images with md5-based file names, some search queries put an src image.
Searching for "1000" will distort
<img src="53451000abababababa---.jpg"
to
<img src="5334<span class="hl">1000</span>abababab--.jpg">
Is it possible to resolve this with a regular expression, somehow excluding anything to ".jpg"?
Or would it be possible before highligting replaces the images with placeholders, and after the replacement comes back to src?
Example:
- replace all <img *> with {{I-01}}, {{I-02}}, etc. and save the real src in var.
- Make the replacement above.
- {{I-01}} < img src= ".." / >
DOM- - , , , , regexp - , .
UPDATE
:
function searchHighlight(searchTxt) {
var stack = new Array();
var stackPtr = 0;
var target = $('#page').html();
target = target.replace(/<img.+?>/gi,function(match) {
stack[stackPtr] = match;
return '{{im' + (stackPtr++) + '}}';
});
var re = new RegExp(searchTxt, 'gi');
target = target.replace(re,'<span class="high">' + searchTxt + '</span>');
stackPtr = 0;
target = target.replace(/{{im.+?}}/gi,function(match) {
return stack[stackPtr++];
});
$('#page').html(target);
}