javascript - Recognizing Multple Sets of Opening Tag, Body, and Closing Tag in Regex -


i'm attempting develop function converts [bold]...[/bold] <b>...</b> forum. of right now, works when there 1 set of [bold]...[/bold], however, whenever second 1 added, regex not recognize end of first [bold] until second.

to illustrate, if put "[bold]hello[/bold], how [bold]today[/bold]?", should this:

hello, how today?

however, i'm getting this:

hello[/bold], how [bold]today?

below current function:

function formatbolds(str) {     output = output.replace(/(\[bold\])(.*)(\[\/bold\])/g, "<b>$2</b>"); } 

all need change .* non greedy .*?

function formatbolds(str) {     output = output.replace(/(\[bold\])(.*?)(\[\/bold\])/g, "<b>$2</b>"); } 
  • the problem .* greedy , tries match many characters possible, includeing first [/bold] , following [bold] until last [/bold]. ? makes non greedy, , halts matching @ minimum length match

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -