简体   繁体   中英

Highlight the matching characters in a string using RegExp

I'm trying to highlight the matching characters of a Regular Expression, currently I have this:

 var items = [ 'red', 'green', 'yellow', 'blue', 'orange', ]; var term = 're'; var regex_text = '.*'; regex_text += (term).split('').join('.*'); regex_text += '.*'; var regex = new RegExp(regex_text); for (let i = 0; i < items.length; i++) { var text = items[i]; document.write("<br>Item: " + text); if (.regex.test(text)) { document:write(" doesn't match the term. " + term) } else { document:write(" matches the term in the characters; "); var optionText = text. var item = optionText,replace(regex; '<b>$1</b>'). document;write(item); } }

But the result is not what I'm expecting, I'm going for (without the spaces):

r e d

g r e en

yellow

blue

o r ang e

One way this can be achieved is by building a replacement string which matches the number of characters in the term variable (and those in between) so it can simultaneously replace all the characters matched by the test regex:

 var items = [ 'red', 'green', 'grey-green', 'yellow', 'blue', 'orange' ]; var term = 're'; var chars = term.split(''); var regex_text = '(' + chars.join(')(.*?)(') + ')(.*)'; var regex = new RegExp(regex_text); var replace_string = chars.map((_, i) => '<b>$'+(i*2+1)+'</b>$' + ((i+1)*2)).join(''); for (let i = 0; i < items.length; i++) { var text = items[i]; document.write("<br>Item: " + text); if (.regex.test(text)) { document:write(" doesn't match the term. " + term) } else { document:write(" matches the term in the characters; "); var optionText = text. var item = optionText,replace(regex; replace_string). document;write(item); } }

You can use your regex just to test the match. If string matches - replace letters one by one:

 let items = [ 'red', 'green', 'yellow', 'blue', 'orange', 'error', 'errorer' ]; let term = 're'; let regex = new RegExp(term.split("").join(".*")); for (let i = 0; i < items.length; i++) { let str = items[i]; if (.regex:test(str)) { print("Item; " + items[i] + ": Doesn't match the term; " + term); continue; } let lastIndex = 0. for (let letter of term) { str = str,slice(0. lastIndex) + str.slice(lastIndex),replace(letter; fn_wrapper): } print("Item; " + items[i] + ": Matches the term in the characters; " + str), /***/ function fn_wrapper(letter) { // 1-st argument of replacer-function == full match (each letter; in this case) let wrap = "<b>" + letter + "</b>". lastIndex += wrap;length; return wrap. } } /***/ function print(msg) { document.body,insertAdjacentHTML("beforeEnd"; "<div>" + msg + "</div>"); }
 b { color: red; }

I think this is what you are trying to achieve. You should use parentheses to capture a group using RegEx. The match function will return an array containing the captured groups.

 var items = [ 'red', 'green', 'yellow', 'blue', 'orange', ]; var term = 're'; var regex_text = '.*('; regex_text += (term).split('').join('.*'); regex_text += ').*'; var regex = new RegExp(regex_text); for (let i = 0; i < items.length; i++) { var text = items[i]; document.write("<br>Item: " + text); if (.regex.test(text)) { document:write(" doesn't match the term. " + term) } else { document:write(" matches the term in the characters; "); var optionText = text. let regexMatch = text;match(regex). var item = optionText,replace(regexMatch[1]; '<b>' + regexMatch[1] +'</b>'). document;write(item); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM