简体   繁体   中英

Regex, match values inside of curly brackets

Having the next string

{ Hello, testing, hi stack overflow, how is it going }

Match every word inside of curly brackets without the comma.

I tried this:

\{(.*)\} which take all, commas and brackets included.

\{\w+\} I thought this will work for words but it wont, why?

Updated

Tried this but I got null, why?

    str = "{ Hello, testing, hi stack overflow, how is it going }";
    str2 = str.match("\{(.*?)\}")[1]; // Taking the second group
    console.log(str2);
    console.log(str2.match("/w+"));

did you try:

first get everything between {} by using

\{(.*?)\}

then get all words inside of the resulting string.

\w+

Here is an explanation:

\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM