正则表达式 - 如何从引号之间提取文本并排除引号

时间:2011-11-03 16:13:13

标签: regex

我需要正则表达式的帮助。我需要创建一个规则来保留引号之间的所有内容并排除引号。例如: 我想要这个...

STRING_ID#0="Stringtext";

......变成了......

Stringtext

谢谢!

3 个答案:

答案 0 :(得分:2)

执行此操作的方法是捕获组。但是,不同语言处理捕获组的方式略有不同。这是Javascript中的一个例子:

var str = 'STRING_ID#0="Stringtext"';
var myRegexp = /"([^"]*)"/g;
var arr = [];

//Iterate through results of regex search
do {
    var match = myRegexp.exec(str);
    if (match != null)
    {
        //Each call to exec returns the next match as an array where index 1 
        //is the captured group if it exists and index 0 is the text matched
        arr.push(match[1] ? match[1] : match[0]);
    }
} while (match != null);

document.write(arr.toString());

输出

Stringtext

答案 1 :(得分:1)

"([^"\\]*(?:\\.[^"\\]*)*)"

我建议阅读有关REGEX的信息 here

答案 2 :(得分:1)

"(.+)"$

Regular expression visualization

Edit live on Debuggex

这在2011年被问到.....

相关问题