const regex = /class-(\d{2}) #Matches class, makes sure that class only is 2 digits - captures class digits
\/([^\/]{3}) #captures first 3 characters that aren't a slash.
\/([^\/]{3}) #Capture 3 characters again.
\/([^\/]{2}) #captures two characters
\/([^\/]{3}) #Captures 3 characters
\/things_to_remember\/ #Matches last piece of string./g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('class-(\\d{2}) #Matches class, makes sure that class only is 2 digits - captures class digits
\\\/([^\\\/]{3}) #captures first 3 characters that aren\'t a slash.
\\\/([^\\\/]{3}) #Capture 3 characters again.
\\\/([^\\\/]{2}) #captures two characters
\\\/([^\\\/]{3}) #Captures 3 characters
\\\/things_to_remember\\\/ #Matches last piece of string.', 'g')
const str = `class-11/xxx/xxx/xx/xxx/things_to_remember/
class-12/xxx/xxx/xx/xxx/things_to_remember/
xxx/class-11/xxx/
class-11/xxx/things_to_remember/xxx
class-11/xxx/`;
// 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