Skip to content

Commit c67da87

Browse files
Add ISBN validator regex and utility function (#1436)
1 parent aad29a3 commit c67da87

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ISBN Validator Regular Expression
2+
3+
This JavaScript module provides a regular expression for validating International Standard Book Numbers (ISBNs). It supports both ISBN-10 and ISBN-13 formats, including variations with or without hyphens and spaces.
4+
5+
## Features
6+
7+
- Validates ISBN-10 and ISBN-13 formats
8+
- Supports ISBNs with or without hyphens/spaces
9+
- Accepts ISBNs with or without the "ISBN" prefix
10+
11+
## Usage
12+
13+
```javascript
14+
const isbnRegex = /^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$/;
15+
16+
function validateISBN(isbn) {
17+
return isbnRegex.test(isbn);
18+
}
19+
20+
// Example usage:
21+
console.log(validateISBN('ISBN 978-0-596-52068-7')); // true
22+
console.log(validateISBN('0-596-52068-9')); // true
23+
console.log(validateISBN('0 512 52068 9')); // false
24+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ISBN Validator
2+
// This regex validates both ISBN-10 and ISBN-13 formats
3+
4+
const isbnRegex = /^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$/;
5+
6+
function validateISBN(isbn) {
7+
return isbnRegex.test(isbn);
8+
}
9+
10+
// Example usage:
11+
console.log(validateISBN('ISBN 978-0-596-52068-7')); // true
12+
console.log(validateISBN('0-596-52068-9')); // true
13+
console.log(validateISBN('0 512 52068 9')); // false

0 commit comments

Comments
 (0)