I think that this regex should work:
/(?s)\<strong\>.+\<\/strong\>/
It validates and matches everything between <strong> tags at regex101.com.
However, it doesn't match anything when I use it in the regex match() method.
var string = "text text <strong>some text</strong> text text";
var re = /(?s)\<strong\>.+\<\/strong\>/;
alert(string.match(re));
This should alert the <strong> tags and everything in between. However, it doesn't work at all.
Why is this, and how can I fix it?
The leading
(?s)is an invalid group in a JavaScript regex. The browser console surely produced an error when you tried that.You don't need to escape
<or>, so this works:Note that if you have several
<strong>elements in a run of text, your regular expression will match everything from the first<strong>to the last</strong>, because the+quantifier is greedy. You can change that: