2

I'm using highlight method to mark words in texts. So, my problem is when i try to highlight little words that can be "sub-words" than others. Ex.:

highlight("a estimativa de tempo", ["tim", "oi"])

And the highlight returns:

"a es<mark>tim</mark>ativa de tempo"

But i need the highlight method to match only entire words. Ex.:

highlight("a operadora tim", ["tim", "oi"]), returning:
"a operadora <mark>tim</mark>"

highlight("Oi anuncia", ["tim", "oi"]), returning:
"a operadora <mark>tim</mark>"

highlight("Operadora Tim declara", ["tim", "oi"]), returning:
"Operadora <mark>Tim</mark> declara"

2 Answers2

1

I was going to recommend @infused's method too, but I'd make one small change:

highlight("this Is a test", [/\bis\b/i])
=> "this <mark>Is</mark> a test"

This way it is case-insensitive and fits all your sample cases.

hattenn
  • 4,371
  • 9
  • 41
  • 80
  • Thank you very much, @hattenn! I was try something similar and this error appears. TypeError - no implicit conversion of Regexp into String: actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in escape' actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in block in highlight' actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in map' actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in highlight' ... – Luan Santos Jan 28 '16 at 20:51
  • @LuanSantos, I checked the documentation, and the RegExp functionality is added in version 4.2.1, check out the docs: http://apidock.com/rails/v4.1.8/ActionView/Helpers/TextHelper/highlight. Is there any chance you can update your rails version? – hattenn Jan 28 '16 at 20:59
  • Thank you, @hattenn! I've upgraded the rails, but now highlight method don't match the words. I'm using like that: `text_marked = highlight(article.text, @keywords.map{ |k| /\b#{k}\b/i })` Where @keywords are the array with words to highlight in text. There are anything wrong with this use? – Luan Santos Jan 29 '16 at 15:13
  • @LuanSantos, I honestly can't see anything wrong with that. Did you make sure that `@keywords` is not empty? Could you tell me the output of `@keywords.map{ |k| /\b#{k}\b/i }`? – hattenn Jan 29 '16 at 19:16
0

The highlight helper accepts regular expressions for the match phrases, so you can wrap each phrase in \b which is a zero-length word boundary matcher:

highlight("this is a test", [/\bis\b/])
=> "this <mark>is</mark> a test"

You'll notice that only the standalone word "is" is highlighted and not the the "is" within the word "this".

infused
  • 24,000
  • 13
  • 68
  • 78
  • Thank you very much, @infused! I was try something similar and this error appears. TypeError - no implicit conversion of Regexp into String: actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in `escape' actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in `block in highlight' actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in `map' actionview (4.1.1) lib/action_view/helpers/text_helper.rb:126:in `highlight' ... – Luan Santos Jan 28 '16 at 20:15
  • Support for regexp phrases was added in rails-4.2.0, so you will need to upgrade Rails before you can use this method. – infused Jan 28 '16 at 21:01
  • Thank you, @infused! I've upgraded the rails, but now highlight method don't match the words. I'm using like that: `text_marked = highlight(article.text, @keywords.map{ |k| /\b#{k}\b/i })` Where @keywords are the array with words to highlight in text. There are anything wrong with this? – Luan Santos Jan 29 '16 at 15:13