Regular expression to replace square brackets with angle brackets

前端 未结 3 454
Happy的楠姐
Happy的楠姐 2021-01-28 19:01

I have a string like:

[a b=\"c\" d=\"e\"]Some multi line text[/a]

Now the part d=\"e\" is optional. I want to convert such type of

3条回答
  •  孤街浪徒
    2021-01-28 19:12

    Would some multiline text include [ and ]? If not, you can just replace [ with < and ] with > using string.replace - no need of regex.

    Update: If it can be anything but [/a], you can replace

    ^\[a([^\]]+)](.*?)\[/a]$
    

    with

    $2
    

    I haven't escaped ] and / in the regex - escape them if necessary to get

    ^\[a([^\]]+)\](.*?)\[\/a\]$
    

提交回复
热议问题