When working in any programming language, you may want to search characters within strings most of the time. It’s very easy to perform the following check; But, what you are searching for may not be equivalent to the entire content of the string. It may just be part of it – at the beginning, or end or just somewhere. In order to achieve this, Regular Expressions (short form – RegEx or RegExp) are used. What are Regular Expressions? These are sequence of characters which define a search pattern that can be used on strings. How to Create a RegEx There are two methods involved in creating regular expressions which are: 1. Calling the constructor function of the RegExp object 2. Using a regular expression literal As seen above, the expression is declared between the slashes for literals while for the constructor function, the expression is declared as an argument of type string. The major difference between these two syntaxes is that expressions (like string template literals) can be used in the constructor function but not in the literal expression because it is fully static. Literal expressions are used when the regex is known before execution. But the first syntax is used when there is need to create a reg during run time (such as after taking user input). The expression ^jav matches strings which begin (^) with “jav“. We’d be looking at more ways of declaring expressions later in this article. Application of Regular Expressions Expressions can be used with test() and exec() methods of the RegExp object. test(): Here, you can use the expression to test a string. It returns true if it matches the string or false if otherwise. exec(): This method returns an array of information (index, input and group) and updates the lastIndex property of the regex object if it matches the string. Otherwise, it returns null. It can be used to search for multiple matches in a string. For these methods, the syntax for application is regex.method(string) . The method is applied to the declared expression and takes an argument of the string which it is tested on. Regex can also be used with match(), search(), replace() and split() methods of the String object. match(): If it matches, it returns […]