title: Highlight multiple words using Regex function
body: <p>I have written this function that highlights a word, using ANSI escape colors. <code>\033[91m</code> is red and <code>\033[39m</code> is "reset".</p>
  
  <pre><code>def highlight(text, keyword):
      text = text.replace(keyword, "\033[91m" + keyword + "\033[39m")
      print text
  
  highlight("This word is red.", "word")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/iiZw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iiZw2.png" alt="enter image description here"></a></p>
  
  <p><strong>Q:</strong> My problem is that the function can't handle multiple keywords to highlight (preferably one could enter any number of words into <code>keyword</code>). It's also not case insensitive. What could I do to remedy this?</p>
  
  <hr>
  
  <p>I guess one option would be to use <code>re.sub</code> and perhaps separate keywords using <code>|</code> and ignorecase by <code>flags=re.I</code>. I have made various attempts, but I'm not getting there.</p>
  
  <p><em>This example correctly highlights the word, but unfortunately discards everything except the word itself. It also can't handle multiple words.</em></p>
  
  <pre><code>def highlight(text, keyword):
      regex = "\033[91m" + re.escape(keyword) + "\033[39m"
      text = re.sub(text, regex, text, flags=re.I)
      print text
  </code></pre>