I try to get a function for setting < b > around catched strings (case insensitive), like this :
bold_string("Hello everyone","o")
> "Hell<b>o</b> every<b>o</b>ne"
bold_string("HEllo evEryonE", "e")
> "H<b>E</b>llo <b>e</b>v<b>E</b>ryon<b>E<b/>"
Now, my function looks like that :
def bold_string(str, search)
str.gsub(/(#{search})/i, '<b>\1</b>')
end
It works perfectly with the previous examples, but not with the words having some accents. There are the results I expect :
bold_string("Petite bête", "e")
> "P<b>e</b>tit<b>e</b> b<b>ê</b>t<b>e</b>"
bold_string("Petite bête", "ê")
> "P<b>e</b>tit<b>e</b> b<b>ê</b>t<b>e</b>"
In other words, I have to find a regex like /search/i, it says "you have to find the word 'search' or the word 'search' with some accents".
edit :
I see I was too simplist with my example... It should works with string and not simply chars :
bold_string("Petite bête", "êt")
> "P<b>et</b>ite</b> b<b>êt</b>e"
Regards
Pierre
edit2 : I used the solution of FJ with this new function
def regex_from_string_including_accents(str)
accents = ['aàâ', 'eéèêë', 'oöô' 'iî']
return str.gsub(/./) do |letter|
accent_group = accents.detect{|group| group.include?(letter)}
accent_group ? "[#{accent_group}]" : letter
end
end
You could do something like the following:
def bold_string(str, search)
h = { "e" => "[eéê]", "a" => "[aáâ]" }
regex = search.gsub(/./) {|s| h.fetch(s, s)}
str.gsub(/(#{regex})/i, '<b>\1</b>')
end
Obviously this just shows you how to get started, you will need to fill h
with additional accented versions of characters.
Example: http://ideone.com/KukiKc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.