javascript – Regex to check a substring is having all numeric or not (in Java script) – Education Career Blog

I am using following code to check first four characters are alphabate or not.

var pattern = new RegExp('^A-Z{4}');
if (enteredID.match(pattern))
    isValidAlphabates = true;
else {
    isValidAlphabates = false;

This is working fine; Now I need to check the next 6 characters (of the entered text) need to be only numeric (0 to 9).

I ve the option to use substring or substr method to extract that part & use the regular experssion: ^0-9{6}.

But is there a better way to do it, without using the substring method here.

,

To clarify, the ^ means “at the start of the string”. So you can combine your two regexes to say “start with four A-Z then have six 6 0-9.”

var pattern = new RegExp('^A-Z{4}0-9{6}');

,

var pattern = new RegExp('^A-Z{4}0-9{6}');

,

var pattern = new RegExp('^A-Z{4}0-9{6}$');

I added $ that means that checked string must end there.

,

You can check the whole string at once

var pattern = new RegExp('^A-Z{4}0-9{6}');

Leave a Comment