const regex = new RegExp('((?:[A-Z-][ ]){9,}[A-Z])\\s+([A-Z \\n\\',.-]+)\\b(?![A-Z]+(?=\\.))\\b(?![a-z\\']|[A-Z.])(.*?)(?=(?:[A-Z][ ]){9,}|This book \\n)', 'sg')
const str = `This is some introduction text.
Here's the table of contents:
ONE
The Boy Who Lived · 1
TWO
The Vanishing Glass · 18
THREE
The Letters from No One · 31
FOUR
The Keeper of the Keys · 46
C H A P T E R O N E
THE BOY WHO LIVED
Mr. and Mrs. Dursley, and their greatest fear was that somebody would discover it.
• 1 •
CHAPTER ONE He couldn’t know that at this very moment, people meeting in secret all over the country were holding up their glasses and saying in hushed voices: “To Harry Potter — the boy who lived!”
• 17 •
C H A P T E R E X A M P L E
IT'S A NEW CHAPTER
WITH. SEVERAL, EDGE-CASES
EDGECASE. The chapter continues...
C H A P T E R T W O
THE VANISHING GLASS
Nearly ten years had passed since the Dursleys Everybody knew that Dudley’s gang hated that odd Harry Potter in his baggy old clothes and broken glasses, and nobody liked to disagree with Dudley’s gang.
• 30 •
C H A P T E R T H R E E
THE LETTERS
FROM NO ONE
The escape of the Brazilian boa constrictor earned with Dudley this summer. . . .”
• 309 •
This book
was art directed by
David Saylor and designed by Becky
Terhune. The art for both the jacket and interior was
created using pastels on toned printmaking paper. The text was
set in 12-point Adobe Garamond, a typeface based on the sixteenth-
century type designs of Claude Garamond redrawn by Robert
Slimbach in 1989. The book was printed and bound
at RR Donnelley & Sons, Willard, Oh.
The production was supervised by
Angela Biola
`;
// 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