Getting content between curly braces in JavaScript with regex

后端 未结 5 1232
無奈伤痛
無奈伤痛 2020-12-09 12:44

I am trying to get content between curly braces with JavaScript. I found this thread: Regex to get string between curly braces "{I want what's between the curly bra

5条回答
  •  眼角桃花
    2020-12-09 12:59

    "{I want what's between the curly braces}".replace(/{(.*)}/, "$1");
    

    this should work, cheers.

    Note: you'll "get everything" in the braces, even if it's an empty string.

    Updated: If you are going to match the first character which in the middle of a string, use "match":

    "how are you {I want what's between the curly braces} words".match(/{(.*)}/)[1];
    

    You can just do:

    console.log("how are you {I want what's between the curly braces} words".match(/{(.*)}/));
    

    then you'll see a list of the match items, exploit them to whatever you want.

    Gory details: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

提交回复
热议问题