Regular Expressions in Strings
Regular expressions are a powerful tool for matching patterns in strings. They are widely used in programming languages and tools for tasks like searching, replacing, and validating strings.
Regular expressions itself are out of scope for this Tour, you can learn more about them in the MDN Web Docs.
In this lesson, we will use regular expressions to validate the format postalCode, phoneNumber and countryCode fields in the JSON object.
1{ 2 "name": "John Doe", 3 "age": 25, 4 "postalCode": "385004", 5 "phoneNumber": "84584898564", 6 "countryCode": "IN" 7}
pattern Keyword
In JSON Schema, you can use the pattern keyword to define a regular expression pattern that a string value must match.
Example
1{ 2 "type": "string", 3 "pattern": "^[0-9]{6}$" 4}
Let's break down the regular expression ^[0-9]{6}$:
- ^ asserts the start of the string.
- [0-9] matches any digit from 0 to 9.
- {6} specifies that the previous pattern should be repeated exactly 6 times.
- $ asserts the end of the string.
Now, try to modify the postalCode, phoneNumber and countryCode fields in the side editor with the below constraints in mind.
Constraints:
- The postalCode should be a string with a length of exactly 6 characters and should contain only digits.
- The phoneNumber should be a string with a minimum length of 10 characters and should contain only digits.
- The countryCode should be a string with a length of exactly 2 characters and should contain only uppercase letters.
Hint: to only allow uppercase letters, you can use the regular expression [A-Z] in your regex pattern.