How do you highlight all the words on the page that match an array of words?
问题 I want to find all the words on my webpage that match any of the words in a Javascript array, and highlight them (wrap them in special span tags). What's the easiest way to do this? I use jquery. 回答1: Not perfect, but simple and may work: var regex = /(Hello|Goodbye)/g; $('*').each(function() { var $this = $(this); var text = $this.text(); if (regex.test(text)) { $this.html( $this.html().replace(regex, '<span>$1</span>') ); } }); http://jsfiddle.net/pdWAn/ 回答2: Try this these methods