React JS Training Course Online
- 20k Enrolled Learners
- Weekend
- Live Class
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:
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.
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*/.
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,
There are different methods of using JavaScript Regex. So let’s move ahead and have a look at the different expressions.
Modifiers are used to perform case-insensitive and global searches.
Modifier | Description |
g | It performs a global match |
i | This performs any case-insensitive matching |
m | It 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
var str = "nthe dog ran after nthe cat"; var patt1 = /^the/m;
Output:
the
Brackets are used to find a range of characters.
Expression | Description |
[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]
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
Unleash your creativity and design skills with our UI and UX Design Course.
Metacharacters are characters with a special meaning.
Metacharacter | Description |
w | It looks for a word character |
W | It finds a non-word character |
d | It finds a digit |
D | It finds a non-digit Character |
s | It finds a whitespace character |
S | It finds a non-whitespace character |
b | It finds a match at the beginning/end of a word |
B | It looks for a match, but not at the beginning/end of a word |
f | It finds a form feed character |
r | It finds a carriage return character |
v | It finds a vertical tab character |
t | It 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;
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
Quantifier | Description |
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;
e,e,e,e,e ell,el,e,e,e ,,,,,,,,,,,,,,,,,,,,,,,,,,,1,,1,,,1,,, 12,12
Property | Description |
Constructor | Returns the function that created the RegExp object’s prototype |
global | Checks whether the “g” modifier is set |
ignoreCase | Checks whether the “i” modifier is set |
lastIndex | Specifies the index at which to start the next match |
multiline | Checks 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;
function RegExp() { [native code] } true true
Method | Description |
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).
Check out the Angular Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.
Got a question for us? Please mention it in the comments section of “JavaScript Regex” and we will get back to you.
Course Name | Date | Details |
---|---|---|
UI UX Design Certification Course | Class Starts on 19th October,2024 19th October SAT&SUN (Weekend Batch) | View Details |
edureka.co