正则表达式用于捕获两个双引号中的文本

时间:2019-06-04 04:38:47

标签: regex regex-lookarounds regex-group

我正在尝试获得一个正则表达式,它将在下面的示例中找到text1text2

,"blabla "test1" blabla", "another text"

,"blabla "test2" blabla", "another text"

总而言之,我希望所有双引号之间的文本,以及双引号和逗号之间的文本。

1 个答案:

答案 0 :(得分:-1)

此表达式可能会这样做:

 ".+?"(.+?)".+?"

,我们想要的输出位于该捕获组中:

 (.+?)

Demo

const regex = /".+?"(.+?)".+?"/gm;
const str = `,"blabla "test1" blabla", "another text"
,"blabla "test2" blabla", "another text"`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}