Regex to Match an ISBN-13
Copy the pattern, paste your text below, and find every ISBN-13 it contains. Below the tester: how the 978/979 prefix works and why the check digit still needs code.
What this pattern matches
This expression matches a modern 13-digit ISBN, with or without the hyphens or spaces that separate its parts — 978-0-13-468599-1 or 9780134685991. All ISBN-13s begin with the 978 or 979 prefix.
How it works, part by part
97[89] — the EAN prefix, 978 or 979.
[-\s]? — an optional hyphen or space between groups.
\d{1,5}, \d+, \d+ — the registration group, registrant, and publication elements, whose lengths vary.
\d — the final check digit.
Why the check digit needs code
An ISBN's last digit is a checksum computed from the other twelve. A regex can confirm the shape but cannot do the modulo-10 arithmetic that proves the number is valid. Match with this pattern to locate ISBNs, then verify the checksum in code.
Use it in your code
import re
text = "See ISBN 978-0-13-468599-1 and 9780134685991. Old form 0-13-468599-7 is ISBN-10."
pattern = r'''97[89][-\s]?\d{1,5}[-\s]?\d+[-\s]?\d+[-\s]?\d'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "See ISBN 978-0-13-468599-1 and 9780134685991. Old form 0-13-468599-7 is ISBN-10.";
const re = new RegExp(String.raw`97[89][-\s]?\d{1,5}[-\s]?\d+[-\s]?\d+[-\s]?\d`, "g");
console.log(text.match(re));
FAQ
Does it match ISBN-10 as well?
No. ISBN-10 has no 978/979 prefix and can end in X. Use \b\d{9}[\dX]\b for the older ten-digit form.
Will it verify the check digit?
No. Verifying the checksum requires the modulo-10 calculation, which a regex cannot perform. Do it in code after matching.
Can ISBNs use spaces instead of hyphens?
Yes. The [-\s]? groups accept either a hyphen, a space, or nothing between the parts.