如何使用正则表达式查看带有引号内文本的字符串,但忽略括号内的任何内容?
How do I look in a string with text inside quotations, but ignoring anything inside brackets using regex?
例如我的字符串是:
var str = 'Hello "Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3] Count these too"';
如何在忽略括号内的字符的情况下获取引号内字符串中的所有内容?
例如要收集的正则表达式:“Counts1”、“Counts2”、“Counts3”、“Count these too”
到目前为止我只得到:
var regex = /".*?"/g;
它输出:
"Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3]"
应该这样做:/(?<="|\] )[\w ]+(?="| \[)/g
正后视 (?<="|\] )
确保 [\w ]+
左侧有 "
或 ]
。
正面前瞻 (?="| \[)
确保 [\w ]+
在右边有 "
或 [
。
您还可以使用以下同样支持嵌套方括号的非正则表达式方法:
const extract = (str) => {
let result = [], start = 0, i=0, level = 0, inquote=false;
for (let i = 0; i < str.length; ++i) {
switch (str[i]) {
case '"':
if (inquote && !level) {
if (start < i) {
result.push(str.substr(start, i - start));
}
start = i + 1;
} else {
start = i+1;
}
inquote = !inquote;
break;
case '[':
if (inquote) {
if (start < i && inquote && !level) {
result.push(str.substr(start, i - start));
}
++level;
}
break;
case ']':
if (inquote) {
if (level > 0) {
--level;
}
if (level == 0) {
start = i + 1;
}
}
break;
}
}
if (start < i)
result.push(str.substr(start, i - start));
return result;
}
console.log(extract('Hello "Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3] Count these too"'));
console.log(extract('Hello "Counts1 [[ignore1] this [2]] Counts2 [ignore2] Counts3 [ignore3] Count these too"'));
console.log(extract('"Counts1 [[ignore1] [2]] Counts2 [ignore2] Count these too" and [junk], and "maybe this, too [not this]"'));
例如我的字符串是:
var str = 'Hello "Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3] Count these too"';
如何在忽略括号内的字符的情况下获取引号内字符串中的所有内容?
例如要收集的正则表达式:“Counts1”、“Counts2”、“Counts3”、“Count these too”
到目前为止我只得到:
var regex = /".*?"/g;
它输出:
"Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3]"
应该这样做:/(?<="|\] )[\w ]+(?="| \[)/g
正后视 (?<="|\] )
确保 [\w ]+
左侧有 "
或 ]
。
正面前瞻 (?="| \[)
确保 [\w ]+
在右边有 "
或 [
。
您还可以使用以下同样支持嵌套方括号的非正则表达式方法:
const extract = (str) => {
let result = [], start = 0, i=0, level = 0, inquote=false;
for (let i = 0; i < str.length; ++i) {
switch (str[i]) {
case '"':
if (inquote && !level) {
if (start < i) {
result.push(str.substr(start, i - start));
}
start = i + 1;
} else {
start = i+1;
}
inquote = !inquote;
break;
case '[':
if (inquote) {
if (start < i && inquote && !level) {
result.push(str.substr(start, i - start));
}
++level;
}
break;
case ']':
if (inquote) {
if (level > 0) {
--level;
}
if (level == 0) {
start = i + 1;
}
}
break;
}
}
if (start < i)
result.push(str.substr(start, i - start));
return result;
}
console.log(extract('Hello "Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3] Count these too"'));
console.log(extract('Hello "Counts1 [[ignore1] this [2]] Counts2 [ignore2] Counts3 [ignore3] Count these too"'));
console.log(extract('"Counts1 [[ignore1] [2]] Counts2 [ignore2] Count these too" and [junk], and "maybe this, too [not this]"'));