Conversation
Created a context script to quickly highlight and count every occurence of a regex in a page. Example: 'regex \d+' to find all numbers.
|
I noticed that when I try to do a second regex search (which is pretty common because I almost never get regexs right on my first try), there is no way to clear the highlights from the first search, and matches cannot be made when they overlap highlights spans or links. For example, "regex hello world" won't highlight "hello world". |
|
That's a good point, I am not sure what the best way to enable that would be: maybe regex hilights could be given a special id and before it performs the regex it could remove all spans of that id? |
|
One way to do it would be to use a css class to apply the highlight style and remove the class from all the highlight elements when new highlights are applied. However, the spans would still be present in the HTML so you would need to alter the regex matching code to work across multiple HTML elements. I think that would require applying the regex to the raw text extracted then look up the corresponding elements to highlight in nodeMappings based on the character offsets of the matches. For example: // To determine the offsets:
myRe = new RegExp(...);
matches = [];
text.replace(myRe, (match, offset)=>{
matches.push({
start: offset,
end: offset + match.length,
text: match
});
// This doesn't actually replace anything.
return match;
});
// To find the elements that need highlighting:
_.where(nodeMappings, ({start, end})=>{
return (match_start >= start && match_start < end);
}); |
Created a context script to quickly highlight and count every occurence of a regex in a page. Example: 'regex \d+' to find all numbers.