0
How can I perform a search in a DIV resembling Chrome’s Ctrl+F with javascript?
Case-independent, lowercase, accents, etc.
So far I have the result below. I take the term found and check if there is occurrence in the text. But when the occurrence has accent, does not highlight (does not find).
I am using this script to have the result in the image.
var searchTerm = "CLAUDIA";
var html = $("#texto").text();
var pattern = "([^\\w]*)(" + searchTerm + ")([^\\w]*)";
var rg = new RegExp(pattern, "gi");
var match = rg.exec(html);
if(match) {
html = html.replace(rg,match[1] + "<b>"+ match[2] +"</b>" + match[3]);
$("#texto").text(html);
}
Specify your question... What have you managed to do? The search will be done through a
input
? Post your code.– Jorge.M
I’ll edit the question to explain it better
– Josivan Sousa
I tested your code and it’s even picking up the accents. See if this is it: https://jsfiddle.net/x28g6qn2/
– Sam
The opposite does not work. For example, let’s take the example of the above img, if the name in the text was "CLAUDIA", it would not find.
– Josivan Sousa
Yes, you would. See: https://jsfiddle.net/x28g6qn2/4/
– Sam
So far I do not understand. Your code is finding everything I test. Maybe you meant that if you typed "Claudia", for example, you would find "Cláudia", "CLAUDIA", "CLÁUDIA", "Claudia" etc... that would be it?
– Sam
@dvd, https://jsfiddle.net/x28g6qn2/9/
– Josivan Sousa
What is the relationship of the question with Vue.js?
– Sergio
Nothing. It doesn’t even matter.
– Josivan Sousa
So that’s what I said. You want to find the word regardless of accents or caps. Only the question says otherwise: "Taking uppercase, lowercase, accents, etc."
– Sam