I am trying to highlight all words from search text using other people's code
The JavaScript code is here:
var s = document.querySelector('input[type="search"]'),
p = document.querySelector('p'),
find = function(){
var words = p.innerText.split(' '),
i = words.length,
word = '';
while(--i) {
word = words[i];
if(word.toLowerCase() == s.value.toLowerCase()){
words[i] = '<span class="highlight">' + word + "</span>";
}
else{
}
}
p.innerHTML = words.join(' ');
}
s.addEventListener('keydown', find , false);
s.addEventListener('keyup', find , false);
However, in this code if want to search some words end with '.', it won't give me correct content. I figure out, it is caused by var words = p.innerText.split(' ') however if I used split(/\b(\w+)\b/g) it will cause the extra space.
How can I use right regular expression to accomplish this using original js?