JavaScript RegExp is an object that represents a regular expression, which is a pattern of characters used to search, replace, and validate strings in JavaScript. The regular expression (RegExp) is an object that describes a pattern of characters.
A regular expression is created using a special syntax that consists of two forward slashes (/
) enclosing the pattern that the regular expression should match.
const regex = /hello/;
We created a regular expression that matches the string “hello”.
Flags are added to the end of the regular expression after the second forward slash. For example, the i
flag makes the regular expression case-insensitive.
const regex = /hello/i;
We created a case-insensitive regular expression that matches the string “hello” regardless of whether it’s uppercase or lowercase.
Regular expressions can include a variety of special characters that have special meanings. Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here’s a list of metacharacters:
[] . ^ $ * + ? {} () \ |
Character | Description |
---|---|
. | Matches any character except for a newline character. |
* | Matches the preceding element zero or more times. |
+ | Matches the preceding element one or more times. |
? | Matches the preceding element zero or one time. |
[] | Matches any character within the brackets. |
^ | Matches the start of the string. |
$ | Matches the end of the string. |
() | Groups elements together. |
| | matches either the expression before or after the pipe character. |
JavaScript RegExp example
Simple example code Validating the Email Address.
<!DOCTYPE html>
<html>
<body>
<script>
function validateEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
console.log(validateEmail('[email protected]'));
console.log(validateEmail('[email protected]'));
console.log(validateEmail('invalid-email'));
</script>
</body>
</html>
Output:
Regular Expression Flags
Flag | Description |
---|---|
g | Matches all occurrences of the pattern within the string, rather than just the first one. |
i | Makes the regular expression case-insensitive, so that it matches both uppercase and lowercase letters. |
m | Allows the regular expression to match the start and end of each line in a multiline string, rather than just the start and end of the entire string. |
JavaScript Regular Expression Methods
Method | Description |
---|---|
test | Tests whether a string matches a regular expression pattern and returns true or false . |
exec | Searches a string for a regular expression pattern and returns an array containing the matched substrings, or null if there is no match. |
match | Searches a string for a regular expression pattern and returns an array containing the matched substrings, or null if there is no match. |
search | Searches a string for a regular expression pattern and returns the index of the first match, or -1 if there is no match. |
replace | Searches a string for a regular expression pattern and replaces the matched substrings with a replacement string. |
split | Splits a string into an array of substrings based on a regular expression pattern. |
Do comment if you have any doubts or suggestions on this JS Regular expression topic,
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version