Parsing BBCode in Javascript

三世轮回 提交于 2019-12-02 01:21:54
Freddy

This does the trick for me: (updated this one too to avoid confusion)

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

See regexpal and enter the following:

[code]
    code....
[/code]

[code]code.... [/code]

Update: Fixed the regex to the following and this works in the Chrome Console for me:

/\[code\]([\s\S]*?)\[\/code\]/g.exec("[code]hello world \n[/code]")
Sukima

JavaScript does not handle multi-line RegExp matches. Instead you have to use the [\s\S] trick described in this SO answer. Perhaps?

/\[code\][\s\S]*\[code\]/

Also RegExps probably isn't the best choice for parsing syntax. It's is extremely over complicated. I would suggest parsing the string and building an Abstract Syntax Tree then rendering the HTML from that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!