Table of Contents
The syntax of the RegExp is:
var newPattern = new RegExp(pattern, modifier(s));
The parameters are described in the following manner. The pattern is a string that specifies the regular expression’s or another regular expression’s pattern. Attributes are optional strings containing one of the “g”, “i” or “m” attributes, which, in turn, denote global, case-insensitive, or multi-line matches.
let text = "Visit codeunderscored"; let pattern = /codeunderscored/i; let result = text.match(pattern); console.log(result);
According to the example above, we draw the following conclusions:
- Codeunderscored – is the pattern we are searching for
- /codeunderscored/ – refers to the regular expression
- /codeunderscored/i – denotes a case-insensitive regular expression.
Generating regular expressions
A specific kind of object is a regular expression. It can be created using the RegExp constructor or by wrapping a pattern in forward slash (/) characters to create a literal value.
let varReOne = new RegExp("xyz"); let varReTwo = /xyz/;
The pattern represented by both of those regular expression objects is: a character followed by a b character and a c character. The standard restrictions for backslashes apply since the pattern is written as a regular string when using the RegExp constructor.
Backslashes are handled slightly differently in the second notation, where the pattern appears between slash characters. First, we must use a backslash before any forward slash that we wish to be a part of the pattern because a forward slash finishes the pattern. Backslashes that do not belong to special character codes, like n, will also be maintained rather than ignored, as they are in strings, changing the pattern’s meaning.
In regular expressions, some symbols, such as question marks and plus signs, have particular meanings and backlashes have to proceed if they stand in for the actual symbol.
let resPlus = /twenty+/;
Check for matches
There are numerous techniques for regular expression objects. The test is the simplest. If you give it a string to work with, it will respond with a Boolean, indicating if the string matches the pattern in the expression.
console.log(/xyz/.test("xyzde")); // → true console.log(/xyz/.test("abxde")); // → false
That string of characters is easily represented by a regular expression made up only of nonspecial characters. The test returns true if the character xyz appears anywhere in the string we are testing against (not just at the beginning).
Case-insensitive and global searches are carried out with the use of modifiers. Many accessible modifiers, such as case sensitivity, searching on multiple lines, etc., can make using regexps simpler.
- g – Perform a global match, i.e., discover every match rather than just the first one
- i – carry out a case-insensitive search
- m – Match several lines.
The usage of brackets () in the context of regular expressions has a specific significance. They are employed to locate various characters. As a result, to find a range of characters, use brackets:
- [abc] locate any character of your choice between the given brackets
- [^abc] Try locating a character that is not between the provided brackets
- [0-9] Locate any digit character between the brackets
- [a-z] Any lowercase letter, starting with a, through z, will match.
- [A-Z] – returns any uppercase character from A to Z that is compatible with it.
- [a-Z] -Any letter from lowercase A to uppercase Z is a match.
- [^0-9] It locates any non-digit character that is not available between the brackets
- (x|y) Find any of the suggested options.
In addition to the broad ranges listed above, you could also use the ranges [0-3] to match any decimal digit from 0 to 3 or [b-v] to match any lowercase character from b to v.
Characters with a specific meaning are called metacharacters. Further, a metacharacter encompasses an alphabetical character that has a backslash before it to give the combination of characters a specific meaning. For instance, the ‘d’ metacharacter can be used to search for a significant amount of money: /([d]+)000/,d will look for any string of numeric characters in this case.
A list of a number of metacharacters used in regular expressions written in the PERL style is provided in the following table.
- . Responsible for finding a singular character. However, it doe not include a line terminator or newline
- w When you want to locate a word character, you use w
- W the W is useful in finding characters that are a non-word
- d d is useful when locating digits
- D When the aim is to find a non-digit character, D is vital
- s Locates a character that is specifically a whitespace
- S S is important for finding characters that are not a whitespace
- b Works both when you want a word to match either at the end or the beginning of a word. For example, use: bCode at the beginning and: Codeb at the end.
- B B comes to play when finding matches that are not at the end or the beginning of a word