2

For example my string is:

var str = 'Hello "Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3] Count these too"';

How would I get everything inside the string that is inside quotations ignoring the characters inside the brackets?

So for example a regex to collect: "Counts1", "Counts2", "Counts3", "Count these too"

So far I only got:

var regex = /".*?"/g;

It outputs:

"Counts1 [ignore1] Counts2 [ignore2] Counts3 [ignore3]"
ZygD
  • 22,092
  • 39
  • 79
  • 102

2 Answers2

1

This should do it: /(?<="|\] )[\w ]+(?="| \[)/g

Positive lookbehind (?<="|\] ) makes sure that [\w ]+ has either " or ] to the left.
Positive lookahead (?="| \[) makes sure that [\w ]+ has either " or [ to the right.

Demo

ZygD
  • 22,092
  • 39
  • 79
  • 102
0

You can also use the following non-regex approach that also supports nested square brackets:

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]"'));
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563