[go: up one dir, main page]

0% found this document useful (0 votes)
34 views16 pages

JavaScript Regex Syntax Guide

Uploaded by

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

JavaScript Regex Syntax Guide

Uploaded by

Aman Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Regular Expression

• Regular expressions are used to do


sophisticated pattern matching, which can
often be helpful in form validation.
• There are two ways to create a regular
expression in JavaScript:
• Using literal syntax
var pattr1 = /pattern/;
• Using the RegExp() constructor
var pattr1 = new RegExp("pattern");
• Regular Expression Syntax
• A caret (^) at the beginning of a regular
expression indicates that the string being
searched must start with this pattern.
Example:- The pattern ^foo can be found in
"food", but not in "barfood".
• A dollar sign ($) at the end of a regular
expression indicates that the string being
searched must end with this pattern.
Example:- The pattern foo$ can be found in
"curfoo", but not in "food".
• A question mark (?) indicates that the
preceding character should appear zero or
one time in the pattern.
Example:- The pattern foo? can be found in
“food" and "fod", but not “fd".
• A plus sign (+) indicates that the preceding
character should appear one or more times in
the pattern.
Example:- The pattern fo+ can be found in
"fod", "food" and "foood", but not "fd".
• A asterisk (*) indicates that the preceding
character should appear zero or more times in
the pattern.
Example:- The pattern fo*d can be found in
"fd", "fod" and "food".
• Curly brackets with one parameter ( {n} )
indicate that the preceding character should
appear exactly n times in the pattern.
Example:- The pattern fo{3}d can be found in
"foood" , but not "food" or "fooood".
• Curly brackets with two parameters ( {n1,n2} )
indicate that the preceding character should
appear between n1 and n2 times in the
pattern.
Example:- The pattern fo{2,4}d can be found
in "food","foood" and "fooood", but not "fod"
or "foooood".
• Curly brackets with one parameter and an
empty second paramenter ( {n,} ) indicate that
the preceding character should appear at least
n times in the pattern.
Example:- The pattern fo{2,}d can be found in
"food" and "foooood", but not "fod".
Common Characters ( . \d \D \w \W \s \S )
• A period ( . ) represents any character except a
newline.
Example:- The pattern fo.d can be found in
"food", "foad", "fo9d", and "fo*d".
• Backslash-d ( \d ) represents any digit. It is the
equivalent of [0-9].
Example:- The pattern fo\dd can be found in
"fo1d", "fo4d" and "fo0d", but not in "food" or
"fodd".
• Backslash-D ( \D ) represents any character
except a digit. It is the equivalent of [^0-9].
Example:- The pattern fo\Dd can be found in
"food" and "foad", but not in "fo4d".
• Backslash-w ( \w ) represents any word
character (letters, digits, and the underscore
(_) ).
Example:- The pattern fo\wd can be found in
"food", "fo_d" and "fo4d", but not in "fo*d".

• Backslash-W ( \W ) represents any character


except a word character.
Example:- The pattern fo\Wd can be found in
"fo*d", "fo@d" and "fo.d", but not in "food".
• Backslash-s ( \s) represents any whitespace
character (e.g, space, tab, newline, etc.).
Example:- The pattern fo\sd can be found in "fo
d", but not in "food".
• Backslash-S ( \S ) represents any character except
a whitespace character.
Example:- The pattern fo\Sd can be found in
"fo*d", "food" and "fo4d", but not in "fo d".
• Square brackets ( [] ) are used to match any
character inside it.
Example 1:- The pattern f[aeiou]d can be
found in "fad" and "fed", but not in "food",
"faed" or "fd".
Example 2:-The pattern f[aeiou]{2}d can be
found in "faed" and "feod", but not in "fod",
"fed" or "fd".
• Negation ( ^ )
• When used after the first character of the
regular expression, the caret ( ^ ) is used for
negation.
Example:- The pattern f[^aeiou]d can be found
in "fqd" and "f4d", but not in "fad" or "fed".
• Subpatterns ()
• Parentheses () are used to capture
subpatterns.
Example:- The pattern f(oo)?d can be found in
"food" and "fd", but not in "fod".
• Alternatives ( | )
• The pipe ( | ) is used to create optional
patterns.
Example:- The pattern foo$|^bar can be
found in "foo" and "bar", but not "foobar".
• Escape Character ( \ )
• The backslash ( \ ) is used to escape special
characters.
Example:- The pattern fo\.d can be found in
"fo.d", but not in "food" or "fo4d".
• Flags
• Flags appearing after the end slash modify
how a regular expression works.
• The i flag makes a regular expression case
insensitive.
Example:- /aeiou/i matches all lowercase and
uppercase vowels.
• The g flag specifies a global match, meaning
that all matches of the specified pattern
should be returned.

You might also like