0% found this document useful (0 votes)
10 views4 pages

JavaScript Regex Guide and Examples

The document explains regular expressions in JavaScript, detailing their syntax, metacharacters, sets, ranges, and quantifiers. It also describes commonly used methods like match() and matchAll() for string validation. Additionally, it provides an example of a JavaScript function that validates user input for various fields using regular expressions.

Uploaded by

kowsikakowsika27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

JavaScript Regex Guide and Examples

The document explains regular expressions in JavaScript, detailing their syntax, metacharacters, sets, ranges, and quantifiers. It also describes commonly used methods like match() and matchAll() for string validation. Additionally, it provides an example of a JavaScript function that validates user input for various fields using regular expressions.

Uploaded by

kowsikakowsika27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Regex : Regular Expressions in JavaScript

Regular expressions are a string of characters that are used to validate the contents of
another string. These strings of characters that form the regular expression are stored in an
object. A regular expression consists of two parts. The first part is the pattern, which is followed
by an optional flag.
Syntax:
var regEx = /pattern/flag
Flags are also referred to as modifiers.

Metacharacters for Regex are as follows:

 \t – Used to find a tab character.


 \v – Used to find a vertical tab character.
 \s – Used to find white space characters.
 \S – Used to find non-whitespace characters.
 \d – Used to find numerical digits.
 \D – Used to find non – numeric digits.
 \w – Used to find words.
 \W – Used to find anything except for words.
 . – A dot is used to find a single character other than the newline or end of the line.
 \0 – Used to find a null character.

Sets and Ranges of Regular Expressions in JavaScript


 [0-9] – All characters from zero through nine.
 [A-Z] – All character from A to Z in upper case.
 [a-z] – All character from a to z in lower case.

Quantifiers of Regular Expressions in JavaScript


These are denoted with the help of special characters. Each special character has a
meaning associated with it. These characters are used along with regular expressions.
A few of the most used quantifiers are:
 * – Matches a string containing zero or more instances.
 + – Matches a string containing one or more instances.
 ? – Matches a string containing zero or one instance.
 {n} – Here, “n” takes in a number. Matches the required regular expression the number of
times mentioned in place of “n”.
 $ – Matches the given expression with the end of the string.
 ^ – Matches the given expression with the beginning of the string.
 ?= – Matches any string with the regex pattern after the equals sign.
 ?! – Matches any string that does not contain the regex pattern after the exclamatory sign.
Methods in Regular Expressions and Strings
Commonly used methods in regular expressions are as follows:
 match() – It’s a String method that looks for a match in a string. If found returns the
match; if not, then returns null.
 matchAll() – It’s a String method that looks for all the matches in a string.

Ex:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function check_data()
{
var uname = [Link];
if([Link](/[a-z]{6}/))
{
[Link]="valid userid";
}
else
{
[Link]="Invalid userid";
}

var pwd = [Link];

if([Link](/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*\W)(?!.* ).{8,16}$/))
{
[Link]="valid pwd";
}
else
{
[Link]="Invalid pwd";
}

var ph = [Link];
if([Link](/[0-9]{10}/))
{
[Link]="valid phone";
}
else
{
[Link]="Invalid phone";
}

var email = [Link];


if([Link](/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,3}$/))
{
[Link]="valid mail";
}
else
{
[Link]="Invalid mail";
}
//dd-mm-yyyy
var dob = [Link];
if([Link](/(^0[1-9]|[1][0-9]|[2][0-9]|3[01])-(0[1-9]|1[0-2])-(\d{4}$)/))
{
[Link]="valid dob";
}
else
{
[Link]="Invalid dob";
}

</script>

</head>
<body>
Name:<input type="text" name="t1" id="t1"> <span id="s1"> </span>
<br><br>
Email:<input type="text" name="t2" id="t2"> <span id="s2"> </span>
<br><br>
Phone:<input type="text" name="t3" id="t3"><span id="s3"> </span>
<br><br>
URL:<input type="text" name="t4" id="t4"><span id="s4"> </span>
<br><br>
PWD:<input type="text" name="t5" id="t5"><span id="s5"> </span>
<br><br>
[Link]:<input type="text" name="t6" id="t6"><span id="s6"> </span>
<br><br>
DOB:<input type="text" name="t7" id="t7"> <span id="s7"> </span>
<br><br>
<input type="button" name="b1" id="b1" value="Check" onclick="check_data()">
</body>
</html>

You might also like