Regex to split BBCode into pieces

前端 未结 4 1947
你的背包
你的背包 2020-11-28 14:24

I have this:

str = \"some html code [img]......[/img] some html code [img]......[/img]\"

and I want to get this:

[\"[img]..         


        
4条回答
  •  情话喂你
    2020-11-28 14:42

    irb(main):001:0> str = "some html code [img]......[/img] some html \
    code [img]......[/img]"
    "some html code [img]......[/img] some html code [img]......[/img]"
    irb(main):002:0> str.scan(/\[img\].*?\[\/img\]/)
    ["[img]......[/img]", "[img]......[/img]"]
    

    Keep in mind that this is a very specific answer that is based on your exact question. Change str by, say, adding an image tag within an image tag, and all Hell will break loose.

提交回复
热议问题