简体   繁体   中英

Need help with regex to convert BBCode tags to HTML tags

i have a Javascript regex.

/\[([^:]*)\]/gi

Basically what it needs to do is to get content of [ ] and place it inside of HTML tag src. Like bbForums do.

Problem is when i have input as [somescr] i get proper results but when i have more than one [somesrc] [someothersrc] in row, Regex matches everything between first and last brackets ( ie. somesrc] [someothersrc )

Can you help me with it?

thanks

What you want is a lazy quantifier. So replace * with *? , and it should work magically.

Try using /\[([^:]*?)\]/gi

For more info, see this SO question .

Use this pattern: \[([^:]*?)\] . Put ? after * to make it lazy.

This should do what you need:

myString.replace(/\[(.*?)\](.*?)\[/\1\]/gi, "<$1>$2</$1>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM