const regex = /‘(?!(?:“[^”\n]*”|[^‘”\n])*’(?![a-z]))/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('‘(?!(?:“[^”\\n]*”|[^‘”\\n])*’(?![a-z]))', 'gmi')
const str = `“double-quote,” and some text
Some text and “double-quote” and an ‘orphan
“double-quote,” some text and ‘matched l/rsquo’
Some text and “double-quote” and an ‘orphan and an ’elided word
“double-quote,” ‘matched l/rsquo’ and an ‘orphan and ’elided word
Some text, “double-quote,” more text, an ‘orphan and a contract’n
“double-quote,” some text, and another “double quote.”
“double-quote,” some text, an ‘orphan and “another double-quote”
“double-quote,” a ‘matched l/rsquo with an “embedded double-quote” in it.’
“double-quote,” a ‘matched l/rsquo with an “embedded double-quote” in it’ and an ‘orphan.
“double-quote,” some text, an ‘orphan, a contract’n, and “another double-quote”
“double-quote,” some text, another “double-quote,” an ‘orphan and a contract’n
“double-quote” and “another double-quote” and an ‘orphan and ‘another lsquo
“double-quote” and some text and an ‘orphan and ‘a matched l/rsquo’
Some text and “double-quote” and an ‘orphan and ‘a matched l/rsquo with a contract’n’
some text and an ‘orphan
some text and an ‘orphan and an ’elided word
some text and an ‘orphan and a contract’n
some text ‘orphan, another ‘lsquo, and more text
some text ‘orphan, a contract’n, another ‘lsquo and ’elided word and more text
some text, an ‘orphan and a “double-quote with ‘matching l/rsquo’” and more text
Some text, “double quote, with an ‘orphan inside.”
Some text, “double quote, with a ‘matched l/rsquo inside.’”
Some text, “double quote, with an ‘orphan and contract’n inside.”
Some text, “double quote, with an ‘orphan and ’elided word inside.”
Some text, “double quote, with an ‘orphan and ‘matched l/rsquo inside.’”
“‘Orphan immediately following double-quote.”
“‘Matching l/rsquo immediately inside double-quote.’”
“‘Orphan immediately following double-quote with ‘matched l/rsquo’ inside.”
“Double quote,” another “double quote with an ‘orphan and ‘matched l/rsquo’”
“double quote,” another “double quote with an ‘orphan and contract’n and ‘matched l/rsquo’ and more text”`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
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}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions