JavaScript / ECMAScript

ISBN-13 in JS

Match 13-digit ISBNs starting with 978 or 979, with optional hyphens or spaces.

Try it in the JS tester →

Pattern

regexJS
\b97[89][\- ]?(?:\d[\- ]?){9}\d\b   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\b97[89][\\- ]?(?:\\d[\\- ]?){9}\\d\\b", "g");
const input = "978-0-306-40615-7";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

The 978 or 979 GS1 prefix anchors the match, followed by nine digits (with optional separators) and a final check digit.

Examples

Input

978-0-306-40615-7

Matches

  • 978-0-306-40615-7

Input

9780306406157

Matches

  • 9780306406157

Same pattern, other engines

← Back to ISBN-13 overview (all engines)