简体   繁体   English

需要正则表达式帮助将 BBCode 标签转换为 HTML 标签

[英]Need help with regex to convert BBCode tags to HTML tags

i have a Javascript regex.我有一个 Javascript 正则表达式。

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

Basically what it needs to do is to get content of [ ] and place it inside of HTML tag src.基本上它需要做的是获取 [ ] 的内容并将其放在 HTML 标签 src 中。 Like bbForums do.就像 bbForums 一样。

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 )问题是当我输入为 [somescr] 时,我得到了正确的结果,但是当我在行中有多个 [somesrc] [someothersrc] 时,正则表达式匹配第一个和最后一个括号之间的所有内容(即 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尝试使用/\[([^:]*?)\]/gi

For more info, see this SO question .有关更多信息,请参阅此 SO 问题

Use this pattern: \[([^:]*?)\] .使用此模式: \[([^:]*?)\] Put ?? after * to make it lazy. *之后让它变得懒惰。

This should do what you need:这应该做你需要的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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