const regex = new RegExp('\\\\([a-zA-Z]+ *|.|\\n)', 'g')
const str = `\\documentclass {book}
\\usepackage{a}
\\begin {document}
\\chapter*{This is the title of a index-absent chapter}
\\I'll try to do some actual \\LaTeX\\ text to se\\footnote {This is text in a footnote.} how it will be matched.
\\chapter{This is {\\bf another} chapter title}
You have to be carefull with accents, like \`\`Espa\\~nol''
and active characters (characters like the quotes above
or the newline character, which act by themselves as a
command).\\
You have to be carefull also with comments (beginning with
the percentage char) % this is one comment.
And of course you'll loose al your math, as in
\$e^{i\\pi} + 1 = 0\$.
\\end{document}
`;
// 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