0

I have written a function in Javascript which highlights words in text using. Takes around 20 lines of code.

highlight(text, pattern)

Here it is: http://jsfiddle.net/sR5wM/1/

Can anyone write a regex that would do the same more efficiently? The highlight function must be case insensitive, and ignore diacritics.

For Example:

highlight("Être ou ne pas être, là est la question", "etre la")

Would yield:

Être ou ne pas être, est la question

Martin Drapeau
  • 1,484
  • 15
  • 16
  • 1
    Related [Programatic Accent Reduction in JavaScript (aka text normalization or unaccenting)](http://stackoverflow.com/questions/227950/programatic-accent-reduction-in-javascript-aka-text-normalization-or-unaccentin) – Alex K. Jun 19 '13 at 16:55
  • Except those ugly regex objects your code looks pretty solid. – Christoph Jun 19 '13 at 17:00

2 Answers2

0

The best way to do is provided by Alex K., IMO I would suggest you to also have a look at the UNICODE based regex.

The below regex may not an optimized and closed to your expectation but will do exactly what you need or at least will give you a starter.

Try this regex:

([\u00C8-\u00CB]tre)|(l[\u00E0-\u00E5\u0061])

Live Demo

Notice the case-insensitive check box.

Don't forget to look at the equivalent unicode here from the list.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
-2

I dont remember the exact syntax of how it would look in javascript, but it would be something like

for(var input : inputs){
    string.regexReplace("("+input+")","<b>\1<\b>")
}
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122