Last active
          December 26, 2015 23:09 
        
      - 
      
- 
        Save hyeonseok/7228816 to your computer and use it in GitHub Desktop. 
    Case-insensitive keyword emphasis. See http://stackoverflow.com/questions/15008611/replace-with-gsub-a-regexp-with-accents
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function emphasisKeyword(keyword, string) { | |
| var h = { e: "[eéê]", a: "[aáâ]" }; | |
| var matched = keyword.match(/[\S\s]/g); | |
| var res = []; | |
| for (var i = 0; i < matched.length; i++) { | |
| if (h[matched[i]]) { | |
| res.push(h[matched[i]]); | |
| } else { | |
| res.push(matched[i]); | |
| } | |
| }; | |
| var re = new RegExp('(' + res.join('') + ')', 'ig'); | |
| return string.replace(re, "<strong>$1</strong>"); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def emphasis_keyword(str, keyword) | |
| h = { "e" => "[eéê]", "a" => "[aáâ]" } | |
| regex = keyword.gsub(/./) {|s| h.fetch(s, s)} | |
| return str.gsub(/(#{regex})/i, '<strong>\1</strong>').html_safe | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
2