punjabi revolution tutorial Javascript regular expression part 2

It is in continuity to the previous video. Here we make the regular expression little more expressive. Below is the complete code.

Line 7: It is regular expression pattern /^[0-9]{5}-[0-9]{7}-[0-9]{1}$/ which means

  • ^ in start means, the pattern matching would start from the beginning and $ means, the pattern matching start from the end. Here both are applied, means, the pattern matching would start simultaneously, both from beginning and end. In simple words, it means that both the contents and the length of the input should match with the regular expression pattern.
  • [], the square brackets are used to show the allowed range of characters. [0-9] means only digits are allowed. if we write as [A-Z], it would mean only upper letters are allowed and [a-z] would mean only small letters allowed. [A-Za-z] would mean, both upper case and lower case alphabets are allowed. Instead of range, we can also write it mentioning the complete list if letters like [ABVFR] means only ABVFR are allowed alphabets, any other alphabets would be rejected.
  • {} the curly brackets specify the frequency of the previous letter. for instance, [VCD]{4} means, only VCD are permissible letters and the word should contain exactly 4 letters. for example VVVV, VCDV, CVDC are few valid examples.
  • Now, lets revisit the above pattern, it says,
    • There should be 5 digits then a hash then 7 digits then  a hash and then 1 digit. no other input is permissible.

Lets take an other scenario. Suppose the input should contain a valid number plate of motor vehicles. The accepted format is  three capital alphabets , then hash and then five digits. According to the aforementioned rules, the pattern for such regular expression would be

/^[A-Z]{3}-[0-9]{5}$/ Below is the complete code.

There are also some short codes to specify the permission character range. like \d means digits only which is equal to [0-9]. so we can also write the above expressions as



Below are few short codes for different scenarios.

short codes for regular expressions
short codes for regular expressions







Try the code and verify the result. Should you have any question, please write in below comments area.

Cheers 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *