const regex = /(?=.*(?:epub|digital|e-?(?:ISBN|book))).*ISBN(?: *13)?:?\s+\K(\d(?:-?\d){12})/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?=.*(?:epub|digital|e-?(?:ISBN|book))).*ISBN(?: *13)?:?\\s+\\K(\\d(?:-?\\d){12})', 'gmi')
const str = `These should match to indicate they are eBook ISBNs:
ISBN 978-1-9821-8584-8 (ebook)
EISBN: 9781101487280
eISBN: 978-1-4406-3132-0
eISBN 9781429902304
Ebook ISBN: 9780593547342
eBook ISBN: 9780804139038
e-ISBN 978-0-765-37686-2
Digital Edition MAY 2017 ISBN: 978-0-06-244198-0
ISBN 13: 978-0-9999999-9-9 (eBook edition)
EPub © Edition JUNE 2003 ISBN: 9780061739200
But then these should not match, as they are assumbed to be print copy ISBNs:
Print ISBN: 978-0-06-244197-3
ISBN 978-1-9821-8582-4
ISBN 9780804139021
ISBN-13: 978-1-4299-6030-4
ISBN 13: 978-0-9999999-9-9 (Paperback edition)`;
// 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