garysieling / jquery-highlighttextarea

http://garysieling.github.io/jquery-highlighttextarea/
156 stars 75 forks source link

Words with vertical bar ('|') does not highlight #65

Open ChenglimEar opened 8 years ago

ChenglimEar commented 8 years ago

If you set the word to highlight to something like '|a', it doesn't work (that's a vertical bar and not a letter).

I've investigated this problem and found two places where it was problematic:

1) I had to use the regular expression for the word such that "|a" became "[|]a". This works such that the initial part of the plugin code matches "|a" in the text area.

2) The second part of the plugin code that actually searches for the match, "|a", to highlight fails because the "|" interferes with the regex being constructed in this line of code:

text = text.replace(new RegExp(match, 'g'), '<mark style="background-color:'+ color +';">$&</mark>', 'g');

Can you fix this? Inserting this line before the above line seems to fix it for me:

match = match.replace('|','[|]');

Thanks.

garysieling commented 8 years ago

The "words" parameter is a list of regexes, which is perhaps a bit unclear. You should be able to escape values you pass in as regexes. I could perhaps add an argument that takes non-regex words, although I'd have to research how to do this in a generalized way.

I did notice that there were rendering issues when a pipe or a > was in the middle of a word, which are now fixed. You can see the changes/new examples here:

https://github.com/garysieling/jquery-highlighttextarea/commit/ffd643ba62bd7c78d0fae35b6fb3d8471b6fd78f#diff-46eb058573e3998a7d10ba211eb90223