javascript - RegEx to find inner content of [code] tags -
if have string value of this:
[code] hi [/code]
i can use regex tag , middle content:
/(?:(\[code\]))([\s\s]*)(?:(\[\/code\]))/gi
but if have string value multiple [code] tag sets returns single match instead of multiple:
[code] hi [/code] [code] hello [/code]
i'm running regex through text.replace parse middle content like:
text.replace(re, function(match, open_tag, middle, close_tag) { //do stuff here return open_tag + middle + close_tag; });
but said, it's not being there 2 separate code sets, single , that's use of \s
matching everything. how parse properly?
quick jsfiddle: http://jsfiddle.net/rutsk28l/
simply use non-greedy variant:
/(?:(\[code\]))([\s\s]*?)(?:(\[\/code\]))/gi
the ungreedy unifier *?
unifies 0 or more, least possible.
you can furthermore omit capturing of [code]
blocks in brackets:
/\[code\]([\s\s]*?)\[\/code\]/gi
making regex bit shorter , more readable.
Comments
Post a Comment