Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Regular Expressions in JavaScript with examples



Hello guys! welcome to my new tutorial post on JavaScript. In this tutorial, i will be rounding up with JavaScript Objects. Like i made mention in my previous tutorial, i will be discussing about the Regular Expression and RegExp object.

A regular expression is an object that describes a pattern of characters.

The RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to carry out powerful pattern-matching(PM) and also search-and-replace functions on text.

Syntax

A RegExp could be defined with the RegExp() constructor, as follows -

var pattern =  new RegExp(pattern,  attributes);
or simply 
var pattern =  /pattern/attributes;

Below is the description of the parameters:

  • pattern - A string that specifies the pattern of the regular expression.
  • attributes - An optional string containing any of the "g", "i", and "m" attributes that specify global, case-insensitive,  and multi-line Matches.

Brackets

Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.

Sr.No.Expression & Description
1
[...]
Any one character between the brackets.
2
[^...]
Any one character not between the brackets.
3
[0-9]
It matches any decimal digit from 0 through 9.
4
[a-z]
It matches any character from lowercase through lowercase z.
5
[A-Z]
It matches any character from uppercase A through uppercase Z.
6
[a-Z]
It matches any character from lowercase a through uppercase Z.
The ranges shown above are general; you could also use the range [ 0-3 ] to match any decimal digits ranging from 0 through 3, or the range [b-v] to match any lowercase character that ranges from b through v.

You can also read our tutorial post on: JavaScript Math Methods

Quantifiers

The frequency or the position of a completely bracketed character sequences  and as well as a single character can be denoted by a special or unique character. Each special character has a specific connotation. The +, *, ?, and $ flags all follow a character sequence.

Sr.No.Expression & Description
1
p+
It matches any string containing one or more p's.
2
p*
It matches any string containing zero or more p's.
3
p?
It matches any string containing at most one p.
4
p{N}
It matches any string containing a sequence of N p's
5
p{2,3}
It matches any string containing a sequence of two or three p's.
6
p{2, }
It matches any string containing a sequence of at least two p's.
7
p$
It matches any string with p at the end of it.
8
^p
It matches any string with p at the beginning of it.

Example

Following example below explains more about matching character.

Sr.No.Expression & Description
1
[^a-zA-Z]
It matches any string not containing any of the characters ranging from a through z and Athrough Z.
2
p.p
It matches any string containing p, followed by any character, in turn followed by another p.
3
^.{2}$
It matches any string containing exactly two characters.
4
(.*)
It matches any string enclosed within and .
5
p(hp)*
It matches any string containing a p followed by zero or more instances of the sequence hp.

You can also read our tutorial post on: The complete guide to JavaScript Math object with examples

Literal Characters

Sr.No.Character & Description
1
Alphanumeric
Itself
2
\0
The NUL character (\u0000)
3
\t
Tab (\u0009
4
\n
Newline (\u000A)
5
\v
Vertical tab (\u000B)
6
\f
Form feed (\u000C)
7
\r
Carriage return (\u000D)
8
\xnn
The Latin character specified by the hexadecimal number nn; for example, \x0A is the same as \n
9
\uxxxx
The Unicode character specified by the hexadecimal number xxxx; for example, \u0009 is the same as \t
10
\cX
The control character ^X; for example, \cJ is equivalent to the newline character \n

Metacharacter

A metacharacter is an alphabetical character preceded by a backlash that acts so to give the combination a special meaning.

For instance, you can search for a huge sum of money using the '\d' metacharacter:/([\d]+)000/, here \d will search for any string of numerical character.

The following table lists a set of metacharacter which can be used in PERL Style Regular Exp.

Sr.No.Character & Description
1
.
a single character
2
\s
a whitespace character (space, tab, newline)
3
\S
non-whitespace character
4
\d
a digit (0-9)
5
\D
a non-digit
6
\w
a word character (a-z, A-Z, 0-9, _)
7
\W
a non-word character
8
[\b]
a literal backspace (special case).
9
[aeiou]
matches a single character in the given set
10
[^aeiou]
matches a single character outside the given set
11
(foo|bar|baz)
matches any of the alternatives specified

You can also read our tutorial post on: JavaScript Events tutorial with examples

Modifiers

Several modifiers are available that can simplify the way you work with regular expressions like case sensitivity, searching in multiple lines, etc.

Sr.No.Modifier & Description
1
i
Perform case-insensitive matching.
2
m
Specifies that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary
3
g
Performs a global matchthat is, find all matches rather than stopping after the first match.


RegExp Properties
Below is the list of the properties associated with the RegExp object and their description.

Sr.No.Property & Description
1constructor
Specifies the function that creates an object's prototype.
2global
Specifies if the "g" modifier is set.
3ignoreCase
Specifies if the "i" modifier is set.
4lastIndex
The index at which to start the next match.
5multiline
Specifies if the "m" modifier is set.
6source
The text of the pattern.

In my next tutorial, i will be using few examples to illustrate the usage of RegExp properties.

RegExp Methods
Below is the list of the methods associated with the RegExp object and their description.

Sr.No.Method & Description
1exec()
Executes a search for a match in its string parameter.
2test()
Tests for a match in its string parameter.
3toSource()
Returns an object literal representing the specified object; you can use this value to create a new object.
4toString()
Returns a string representing the specified object.

In my subsequent tutorials, i will be using few examples to illustrate the usage of RegExp Methods. 

Alright guys, thats it for this tutorial on RegExp object. In my next tutorial post,  i will be using few examples to demonstrate the proper usage of the RegExp properties which i listed out in this tutorial.

Feel free to ask your questions in any area that you are finding difficult to understand. Follow us on our various social media platforms and also subscribe to our newsletter.

Thanks for reading and bye for now.


This post first appeared on Web Design Tutorialz, please read the originial post: here

Share the post

Regular Expressions in JavaScript with examples

×

Subscribe to Web Design Tutorialz

Get updates delivered right to your inbox!

Thank you for your subscription

×