Lesson 15 of 15By Chinmayee Deshpande
Last updated on Aug 6, 20201151Regular Expressions are patterns that match a combination of characters in a string. JavaScript supports regular expressions mostly for text search and text replacement. In this article on JavaScript RegEx, the following topics are covered:
A JavaScript RegEx is a sequence of characters that forms a search pattern. You can define what needs to be searched in a text with the help of regular expressions. These expressions can be of any number of characters, be it alphabets, digits or special characters. They are more commonly used for text search and text replacement operations.
General Syntax:
/pattern/flag
The syntax consists of two forward-slashes separated by a character pattern and ending with a flag.
For the effective working of Regular Expressions, flags, patterns, and quantifiers are crucial. Let us try to understand them one by one.
Flags alter the behavior of JavaScript RegEx. They may be appended to the end of a RegEx literal, or they may be specified as the second argument to the regular expression. Here’s a list of flags or modifiers supported by JavaScript.
In the example presented above, the pattern provided was the word “Welcome”. As you can see, the word is highlighted in the text. The flag ‘g’ is used to indicate that it highlights all the matches in the text for the word ‘Welcome’.
In this example, the flag “i” is used to indicate case insensitivity. Although the pattern is in the lower case, the string gets matched in the text.
The patterns in JavaScript RegExs suggest the range of characters that are to be searched for. They are enclosed within brackets, as shown below:
In this example, all the digits are matched.
If you closely observe, the upper case letters are not matched. This is because the flag “i” is not used. All the lowercase letters in the alphabet and digits are matched.
A ‘^’ (caret) symbol indicates that the characters following it should not be matched with the text. In the example above, all the lowercase letters are ignored. However, all the digits, whitespaces, and uppercase letters are matched in the text.
Quantifiers define the number of occurrences of a string. The most commonly used quantifiers are ‘+’, ‘*’ and ‘?’.
+ - Indicates one or more occurrence of the character n
* - Indicates zero or more occurrences of the character n
? - Indicates zero or one occurrence of the character n
In the example shown above, we are looking for the character ‘a’ in the string and rightly so, they are highlighted.
Although the quantifier ‘*’ indicates zero or more occurrences of the character ‘a’, it is followed by the character ‘b’. As a result, the letter ‘a’ in the first text does not get matched since it is not followed by the letter ‘b’.
In this example, all strings with zero or more occurrences of ‘a’, followed by zero or more occurrences of ‘b’ are matched. Hence, the letter ‘a’ in the first text gets matched.
Characters with special meaning are known as metacharacters. Given below is a list of a couple of metacharacters and their descriptions.
. |
Finds a character except for newline or a line terminator |
\w |
Matches any word character |
\W |
Matches any word that is not a word character |
\s |
Matches any whitespace character |
\S |
Matches any character that is not a whitespace (tab, spaces, line breaks) |
In the above example, all the word characters are matched.
All the characters that are not words are matched.
All whitespace characters are matched.
All the digits are matched.
Now that you have understood the implementation of JavaScript RegExs, let us learn about how to search for a phone number using these expressions.
Consider an eight-digit telephone number with a three-digit code in the beginning. It can be written in two ways:
XXX - XXXXXXXX or XXXXXXXXXXX where X represents a digit.
A single expression can be written that matches both these strings.
Here, the number in the curly brackets indicates the number of digits to match. The presence of ‘?’ quantifier indicates zero of one occurrence of the preceding token, i.e., hyphen. Both the strings get matched for the same character pattern.
Master the complete JavaScript fundamentals, jQuery, Ajax, and more with the Javascript Certification Training Course. Check out the course preview!
This JavaScript RegEx article introduced you to Regular Expressions. To learn more about JavaScript and make a career as a developer, certification is highly recommended and could act as a catalyst to your coding career. Simplilearn's JavaScript Certification Training course helps individuals master the JavaScript programming language in an all-inclusive training program that includes complete JavaScript fundamentals, jQuery, Ajax, and more. You will also have the opportunity to apply your skills by building a real-time chat application.
If you have any questions or feedback, let us know in the comments section. Our experts will get back to you at the earliest.
Name | Date | Place | |
---|---|---|---|
Full Stack Java Developer | Cohort starts on 10th Mar 2021, Weekend batch | Your City | View Details |
Full Stack Java Developer | Cohort starts on 22nd Mar 2021, Weekend batch | Chicago | View Details |
Chinmayee is a Research Analyst and a passionate writer. Being a technology enthusiast, her thorough knowledge about the subject helps her develop structured content and deliver accordingly.
Full Stack Java Developer
*Lifetime access to high-quality, self-paced e-learning content.
Explore CategoryJava Programming: The Complete Reference You Need
Blockchain Career Guide: A Comprehensive Playbook To Becoming A Blockchain Developer
Introducing Simplilearn’s Full Stack Java Developer Master’s Program
Java EE Tutorial: All You Need To Know About Java EE
All You Need to Know to Implement JUnit Testing in Java
Free eBook: Salesforce Developer Salary Report