I'm doing text matching on an Autocomplete in JQuery, by overloading the _renderItem method to check the JSON object for the text the user searched for. Upon finding it, I'm replacing it with a span tag with a "user_highlight_match" class.
I'm doing multi word searching, so "John Smi" would highlight John, then Smi, and searching "John James Smi" would highlight "John" and "Smi". The problem with this is that, say you search for "John Las", it will start matching against the class attribute of the span tag, if it's already there. How can I get around this?
Edit: Here's some code showing what I'm talking about:
renderItem = function(ul, item) {
li = $('<li>', id: item._id);
if this.term and not item.dialog
terms = this.term.split(' ');
for term in terms
re = new RegExp("("+term+")", 'i');
match = re.exec(item.name);
if match
item.name = item.name.replace re, "<span class="user_highlight_match">+match[0]+</span>";
)
So the first term will match fine, but every time around after that, if you're searching for anything in the html tags, the partial match is replaced with a tag. So say there was a match on "las", it would become this:
<span c<span class="user_highlight_match">Last</span>s="user_highlight_match">Last</span>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…