简体   繁体   English

正则表达式,匹配大括号内的值

[英]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? \{\w+\}我认为这适用于文字,但它不会,为什么?

Updated更新

Tried this but I got null, why?试过这个,但我得到了 null,为什么?

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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