JavaScript / ECMAScript

ISBN-10 in JS

Match 10-digit ISBNs, allowing optional hyphens or spaces between groups.

Try it in the JS tester →

Pattern

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

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\b(?:\\d[\\- ]?){9}[\\dXx]\\b", "g");
const input = "0-306-40615-2";
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

Nine digits each followed by an optional hyphen or space, then a final check character which can be a digit or X (upper or lower case).

Examples

Input

0-306-40615-2

Matches

  • 0-306-40615-2

Input

0306406152

Matches

  • 0306406152

Input

080442957X

Matches

  • 080442957X

Same pattern, other engines

← Back to ISBN-10 overview (all engines)