This answer is mostly to give you some insight into why your current approach does not work, and how you generally solve it.
The reason m
doesn't help is that the other answer is wrong. This is not what m
does. m
simply makes the anchors match line beginnings and endings in addition to the string beginnings and endings. Some regex flavors have s
for what you want to accomplish, but not ECMAScript. The simplest thing (and general solution) is to replace .
(which matches everything except line breaks) with [\s\S]
(which matches whitespace and non-whitespace, i.e. everything).
However, Casimir's approach is better in your case, as it avoids some other problems like greediness. Of course, as Casimir said, if there are tags in between the opening and closing <i>
tags, then the approach will not work. In that case, something like <i>([\s\S]+?)</i>
might be an option, but that's still not the full solution, in case you have nested i
-tags or attributes in the opening tag, or capitalized I
-tags and whatnot.
All in all, using regex to parse HTML is wrong! You should really use DOM manipulation. Especially, since you are using Javascript - THE language for DOM manipulation. What you should really do is traverse the DOM for all i
tags in your demo
element, and replace them with their inner HTML.