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 

regex101 demo

the ungreedy unifier *? unifies 0 or more, least possible.

you can furthermore omit capturing of [code] blocks in brackets:

/\[code\]([\s\s]*?)\[\/code\]/gi 

regex101 demo

making regex bit shorter , more readable.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -