- \s - \s matches any whitespace character
- . - match any character
-
-
- matches the previous token between one and unlimited times.
-
- ^ - asserts position at start of a line
- \w - matches any word character (equivalent to [a-zA-Z0-9_])
- \d - matches a digit (equivalent to [0-9])
- \w+@(?:google.\com) - ?: this represents to turn capture group off
⌈colou?r⌋ - The metacharacter ⌈?⌋ (question mark) means optional.
Command | Description |
---|---|
\d |
Match for a single digit |
\D |
Do not match \d |
\w |
Match for a single letter or digit (0-9 or a-z or A-Z) |
\W |
Do not match \w |
\s |
Match a single space (space, tab, newline characters included) |
\S |
Do not match \s |
[A-D] |
Match a single character in the A-D range |
[^A-D] |
Don't match a single character in the A-D range |
. |
A single wildcard |
? |
Match previous character 0 or 1 time |
* |
Match previous character 0 or more times |
+ |
Match previous character 1 or more times |
{3} |
Match previous character 3 times |
{1,3} |
Match previous character between 1 and 3 times |
^ |
Beginning of a string |
$ |
End of a string |
[] |
Match characters in this bracket range |
[^] |
Don't match characters in this range |
` | ` |
() |
Capture groupings |
(?:) |
Turns capture groupings function off |
pattern | Description |
---|---|
4?986 |
Here the 4 is optional but now you see we are matching on anything that either has 4 or doesn't have it. |
a{2, 4} |
matching 2 or 3 or 4 times but not matching one time. |
const numberPattern = /\d{6}/gm;
return numberPattern.test(123456);
const numberPattern = /[a]{3}/gm;
'aaah, this is practice place'.match(numberPattern);
const numberPattern = /^H.+!/gm;
'Hello world! This is regex.'.match(numberPattern);
const numberPattern = /[a-h]/gm;
'aaah, this is practice place'.match(numberPattern);
const numberPattern = /[a|h]/gm;
'aaah, this is practice place'.match(numberPattern);
const numberPattern = /\d+/g;
'something102asdfkj1948948'.match(numberPattern);
const reURLInformation = new RegExp(
[
"^(https?:)//", // protocol
"(([^:/?#]*)(?::([0-9]+))?)", // host (hostname and port)
"(/{0,1}[^?#]*)", // pathname
"(\\?[^#]*|)", // search
"(#.*|)$" // hash
].join("")
);
const match = item.url.match(reURLInformation);
const re = /^\S+@+\S+\.+\S+/;
return re.test('a@gmail.com');
const emailReg = RegExp(
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/gm
);
if (!emailReg.test(email)) {}
const re = /^(\S+)@([a-zA-Z]+\.(com|org|edu|gov))$/;
return re.test('a@gmail.com');
const emailReg = RegExp(
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/gm
);
if (!emailReg.test(email)) {}
const re = /^[(][0-9]{3}[)][0-9]{3}-[0-9]{4}/;
return re.test(9033558036);
const re = /d{3}-\d{3}-\d{4}/;
return re.test(903-355-8036);
const re = /^(?:\+1[-. ]?)?(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/gm;
return re.test(903-355-8036);
const usernameReg = RegExp(/^[a-z0-9_-]{4,12}$/);
if (!usernameReg.test(username)) {}
const passwordReg = RegExp(/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/);
if (!passwordReg.test(password)) {}
const re = /\w{5}\s\w{5}\!/;
return re.test('Hello World!');
const re = /^Hello World!$/;
return re.test('Hello World!');
const re = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
return re.test('192.168.1.1');
const re = /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
return re.test('192.168.1.1');