RegExp object in JavaScript
Introduction
A regular expression is a concatenation of characters that forms a search pattern. This search pattern or regular expression can be used for text search and text replace operations. In this article we will see the RegExp object uses and different methods.
What Is a Regular Expression?
- A regular expression is a concatenation of characters that forms a search pattern.
- When we search for data in a text, we can use this search pattern to describe what you are searching for.
- A regular expression can be a single character, or a more complicated pattern.
- Regular expressions can be used to conduct all types of text search and text replace operations.
Syntax
/pattern/modifiers;
Example
Explanation
/niceonecode/i is a regular expression.
niceonecode is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-insensitive).
Modifiers
Following modifiers are available:
- i -perform case-insensitive matching
- g -perform a global match (find all matches rather than stopping after the first match)
- m -perform multiline matching
RegExp Object Methods
- compile() -deprecated in version 1.5. Compiles a regular expression
- exec() -tests for a match in a string. Returns the first match
- test() -tests for a match in a string. Returns true or false
- toString() -returns the string value of the regular expression
Brackets
Brackets are used to find a range of characters:
- [abc] -Find any character between the brackets
- [^abc] -Find any character NOT between the brackets
- [0-9] -Find any digit between the brackets
- [^0-9] -Find any digit NOT between the brackets
- (x|y) -Find any of the alternatives specified
Metacharacters
Metacharacters are characters with a special meaning:
- . -Find a single character, except newline or line terminator
- \w -Find a word character
- \W -Find a non-word character
- \d -Find a digit
- \D -Find a non-digit character
- \s -Find a whitespace character
- \S -Find a non-whitespace character
- \b -Find a match at the beginning/end of a word
- \B -Find a match not at the beginning/end of a word
- \0 -Find a NUL character
- \n -Find a new line character
- \f -Find a form feed character
- \r -Find a carriage return character
- \t -Find a tab character
- \v -Find a vertical tab character
- \xxx -Find the character specified by an octal number xxx
- \xdd -Find the character specified by a hexadecimal number dd
- \uxxxx -Find the Unicode character specified by a hexadecimal number xxxx
Quantifiers
- n+ -Matches any string that contains at least one n
- n* -Matches any string that contains zero or more occurrences of n
- n? -Matches any string that contains zero or one occurrences of n
- n{X} -Matches any string that contains a sequence of X n's
- n{X,Y} -Matches any string that contains a sequence of X to Y n's
- n{X,} -Matches any string that contains a sequence of at least X n's
- n$ -Matches any string with n at the end of it
- ^n -Matches any string with n at the beginning of it
- ?=n -Matches any string that is followed by a specific string n
- ?!n -Matches any string that is not followed by a specific string n
Example
Using test()
The test() method is a RegExp expression method.
It searches a string for a pattern and returns true or false, depending on the search result.
The following example searches a string for the character "n":
Since there is an "n" in the string, the output of the code above will be:
Using exec()
The exec() method is a RegExp expression method.
It searches a string for a specified pattern, and returns the found text.
If no match is found, it will return null.
The following example searches a string for the character "e":
Since there is an "e" in the string, the output of the code above will be:
Conclusion
In this article we see the regular expression and its uses. I hope it will help you to create own regular expression.