JavaScript and JQuery Essentials Trainin... (52 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (27 Blogs)
  • jQuery UI Development (2 Blogs)
  • The Complete WebDeveloper (37 Blogs)
  • ReactJS (7 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

JavaScript Regex – Important Regular Expressions You need to Know

Published on Jul 17,2019 90 Views
Sayantini
A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Parsing and extracting data from text or validating texts to a specific pattern is an important requirement in programming. JavaScript uses regular expressions to describe a pattern of characters. This JavaScript Regex article will list out the different methods of using expressions in the following sequence:

 

What are Regular Expressions?

A Regular Expression is a sequence of characters that constructs a search pattern. When you search for data in a text, you can use this search pattern to describe what you are looking for.

 

regular expression - javascript regex - edureka

 

A regular expression can be a single character or a more complicated pattern. It can be used for any type of text search and text replace operations. A Regex pattern consist of simple characters, such as /abc/, or a combination of simple and special characters, such as /ab*c/ or  /example(d+).d*/.

 

JavaScript Regex

In JavaScript, a regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods. It uses regular expressions to perform pattern-matching and search-and-replace functions on text.

Syntax:

A regular expression is defined with the RegExp () constructor as:


var pattern = new RegExp(pattern, attributes);

or simply


var pattern = /pattern/attributes;

Here,

  • Pattern – A string that specifies the pattern of the regular expression or another regular expression.
  • Attributes – An optional string containing attributes that specify global, case-insensitive, and multi-line matches.

There are different methods of using JavaScript Regex. So let’s move ahead and have a look at the different expressions.

 

Modifiers

Modifiers are used to perform case-insensitive and global searches.

ModifierDescription
gIt performs a global match
iThis performs any case-insensitive matching
mIt performs multiline matching

Let’s take an example and see how these modifiers are used in JavaScript.

g modifier:


let str = "This is the example";
let pattern = /is/g;

Output:

is,is

i modifier:


let str = "Welcome to Edureka";
let pattern = /edureka/i;

Output:

Edureka

m modifier:


var str = "nthe dog ran after nthe cat";
var patt1 = /^the/m;

Output:

the

 

Brackets

Brackets are used to find a range of characters.

ExpressionDescription
[abc]It finds any character between the brackets
[^abc]It finds any character NOT between the brackets
[0-9]This finds any digit between the brackets
[^0-9]It finds any non-digit NOT between the brackets

 

Example:


var str = "Edureka Online 123";
var ex1 = /[e]/gi;    //[abc]
var ex2 = /[^e]/gi;   //[^abc]
var ex3 = /[2]/g;      //[0-9]
var ex4 = /[^2]/g;     //[^0-9]

Output:

E,e,e
d,u,r,k,a,O,n,l,i,n,1,2,3
2
E,d,u,r,e,k,a,O,n,l,i,n,e,1,3

 

Metacharacters

Metacharacters are characters with a special meaning.

MetacharacterDescription
wIt looks for a word character
WIt finds a non-word character
dIt finds a digit
DIt finds a non-digit Character
sIt finds a whitespace character
SIt finds a non-whitespace character
bIt finds a match at the beginning/end of a word
BIt looks for a match, but not at the beginning/end of a word
fIt finds a form feed character
rIt finds a carriage return character
vIt finds a vertical tab character
tIt finds a tab character

Let’s take an example to see how these metacharacters are used:


var str = "100% Genuine";
var pattern1 = /w/g;
var pattern2 = /W/g;
var pattern2 = /d/g;
var pattern2 = /D/g;
var pattern2 = /s/g;
var pattern2 = /S/g;

Output:

1,0,0,G,e,n,u,i,n,e
%
1,0,0
%,G,e,n,u,i,n,e
1,0,0,%,G,e,n,u,i,n,e

Quantifiers

QuantifierDescription
n+It matches any string that contains at least one n
n*It matches any string that contains zero or more occurrences of n
n?It matches any string that contains zero or one occurrence of n
n{X}It matches any string that contains a sequence of X n’s
n{X,Y}It matches any string that contains a sequence of X to Y n’s
n{X,}It matches any string that contains a sequence of at least X n’s
n$It matches any string with n at the end of it

 

Let’s take an example to see how these Quantifiers are used:


var str = "Hello, welcome to edureka! 1 12 123";
var quant1 = /e+/g;
var quant2 = /el*/g;
var quant3 = /1?/g;
var quant4 = /d{2}/g;

Output:

e,e,e,e,e
ell,el,e,e,e
,,,,,,,,,,,,,,,,,,,,,,,,,,,1,,1,,,1,,,
12,12

 

Object Properties

PropertyDescription
ConstructorReturns the function that created the RegExp object’s prototype
globalChecks whether the “g” modifier is set
ignoreCaseChecks whether the “i” modifier is set
lastIndexSpecifies the index at which to start the next match
multilineChecks whether the “m” modifier is set

Let’s take an example to see how these object properties are used:


var pattern1 = new RegExp("Welcome to Edureka", "g");
var result1 = pattern1.constructor;
var str = "Hello World!";
var pattern2 = /Wor/g;
var result2 = pattern2.global;
var pattern3 = /hel/i;
var result3 = pattern3.ignoreCase;

Output:

function RegExp() { [native code] }
true
true

 

Object Methods

MethodDescription
compile()It compiles a regular expression
exec()It tests for a match in a string and returns the first match
test()It tests for a match in a string and returns true or false
toString()It returns the string value of the regular expression

exec() method:


var str = "Edureka online courses";
var method1 = new RegExp("e");
var result = method1.exec(str);

Output:

e

test() method:


var str = "Edureka online courses";
var method1 = new RegExp("e");
var result = method1.exec(str);

Output:

true

toString() method:


<span>var method2 = new RegExp("Welcome to edureka", "g");</span>
<span>var result = method2.toString();</span>

Output:

/Welcome to edureka/g

 

These were some of the different methods to define JavaScript Regex. With this, we have come to the end of our article. I hope you understood what are JavaScript Regex and the different methods to define expressions.

Now that you know about JavaScript Function, check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you Learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3). 

Got a question for us? Please mention it in the comments section of “JavaScript Regex” and we will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.