JavaScript Split around curly braces

前端 未结 5 2129
再見小時候
再見小時候 2021-02-09 09:40

I have a string format inside of a string variable:

\"{0} Hello World {1}\"

I need to split it into something like this:

\"{0}\         


        
5条回答
  •  我在风中等你
    2021-02-09 10:24

    You can just filter out the empty strings if you want:

    var s = "{0} Hello World {1}";
    s.split(/({\d+})/).filter(Boolean);
    

    which returns

    ["{0}", " Hello World ", "{1}"]
    

提交回复
热议问题