Regex
regex
December 16, 20223 min read

Regex for India Passport validation

India Passport is an official travel document issued by the Government of India to its citizens for international travel. It is a document that certifies the identity and nationality of the bearer. It facilitates the bearer to travel to foreign countries, and to return to India. It also serves as proof of Indian citizenship. In this article let's understand how we can create a regex for India Passport and how regex can be matched for India Passport.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of India Passport

  • It should be eight characters long.
  • The first character should be an uppercase alphabet.
  • The next two characters should be a number, but the first character should be any number from 1-9 and the second character should be any number from 0-9.
  • It should be zero or one white space character.
  • The next four characters should be any number from 0-9.
  • The last character should be any number from 1-9.

Regex for checking if India Passport is valid or not

Regular Expression-

/^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]$/gm

Test string examples for the above regex-

Input StringMatch Output
Q20 DSADGdoes not match
D6143421matches
X614123421does not match
B34 23411matches

Here is a detailed explanation of the above regex-

/^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]$/gm

Match a single character present in the list below [A-PR-WYa-pr-wy]
A-P matches a single character in the range between A (index 65) and P (index 80) (case insensitive)
R-W matches a single character in the range between R (index 82) and W (index 87) (case insensitive)
Y matches the character Y with index 8910 (5916 or 1318) literally (case insensitive)
a-p matches a single character in the range between a (index 97) and p (index 112) (case insensitive)
r-w matches a single character in the range between r (index 114) and w (index 119) (case insensitive)
y matches the character y with index 12110 (7916 or 1718) literally (case insensitive)
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

Hope this article was useful to match India passport regex pattern.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
Next Article
December 1, 20224 min read