Parsing BBCode in Javascript

前端 未结 2 652
旧巷少年郎
旧巷少年郎 2021-01-22 02:13

I am using this (http://coursesweb.net/javascript/convert-bbcode-html-javascript_cs) as my script for parsing BBCode. I have extended the BBCodes that it can process, however I

相关标签:
2条回答
  • 2021-01-22 02:26

    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.

    0 讨论(0)
  • 2021-01-22 02:43

    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]")
    
    0 讨论(0)
提交回复
热议问题