how to add markup to text using JavaScript regex

Posted by Richard on Stack Overflow See other posts from Stack Overflow or by Richard
Published on 2010-03-15T09:24:36Z Indexed on 2010/03/15 9:29 UTC
Read the original article Hit count: 226

Filed under:
|
|

I need to add markup to some text using JavaScript regular expressions. In Python I could do this with:

>>> import re
>>> re.sub('(banana|apple)', r'<b>{\1}</b>', 'I have 1 banana and 2 apples!')
'I have 1 <b>{banana}</b> and 2 <b>{apple}</b>s!'

What is the equivalent in JavaScript? string.replace(regex, newstring) seems to only take a raw string for replacing.

© Stack Overflow or respective owner

Related posts about regex

Related posts about JavaScript