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
Post a Comment