ISBN Validation Regular Expression
An ISBN (International Standard Book Number) is a unique identifier for books. There are two formats: ISBN-10 (10 digits, with the last digit potentially being X) and ISBN-13 (13 digits starting with 978 or 979). These regular expressions validate the format of ISBN numbers without hyphens or spaces. ISBNs are a specialized type of number pattern.
ISBN-10 Validation
ISBN-10 consists of 10 digits where the last digit can be X (representing 10). This format was used before 2007 and is still found on older books.
Explanation
^- Start of the string.(?:...)- Non-capturing group containing two alternatives.\d{9}X- Nine digits followed by the letter X (uppercase).|- Or operator separating the two alternatives.\d{10}- Exactly 10 digits.$- End of the string.
Implementation
Test Cases
| ISBN-10 | Valid |
|---|---|
| 0306406152 | |
| 0345391802 | |
| 043942089X | |
| 156619909X | |
| 0471958697 | |
| 12345 | |
| 123456789 | |
| 12345678901 | |
| 123456789A | |
| X123456789 | |
| 12345X6789 | |
| abcdefghij |
ISBN-13 Validation
ISBN-13 consists of 13 digits and always starts with either 978 or 979. This format has been the standard since 2007 and is used for all new book publications.
Explanation
^- Start of the string.97- The ISBN-13 prefix always starts with 97.[89]- The third digit must be either 8 or 9 (forming 978 or 979).\d{10}- Exactly 10 more digits to complete the 13-digit ISBN.$- End of the string.
Implementation
Test Cases
| ISBN-13 | Valid |
|---|---|
| 9780306406157 | |
| 9780345391803 | |
| 9780439420891 | |
| 9781566199094 | |
| 9780471958697 | |
| 978030640615 | |
| 97803064061578 | |
| 9770306406157 | |
| 9880306406157 | |
| 123456789012 | |
| 978-0-306-40615-7 | |
| abcdefghijklm |
Note
These regular expressions only validate the format of ISBN numbers. For complete validation, you should also verify the check digit using the appropriate algorithm. ISBN-10 uses modulo 11, while ISBN-13 uses modulo 10. Additionally, ISBNs are often formatted with hyphens (e.g., 978-0-306-40615-7), but these patterns match only the raw numeric format without separators.