Javascript RegExp
In this chapter, we will learn about Regexp.
Regular expressions (Regex) are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec and test methods of RegExp, and with the match, replace, search, and split methods of String. This chapter describes JavaScript regular expressions. It can be a single character or a more complicated pattern.
Syntax
In Javascript, we can create regex in 2 ways.
- literal way
- Constructor function way
Let's see how we can use those ways.
Using a regular expression literal, as follows:
/pattern/modifiers;
Calling the constructor function of the RegExp object, as follows:
var variable = new RegExp(patern);
Regex Patterns
A regular expression pattern is composed of simple characters, such as /ABC/, or a combination of simple and special characters, such as /ab*c/ or /Chapter (\d+)\.\d*/. The last example includes parentheses, which are used as a memory device. The match made with this part of the pattern is remembered for later use.
As we say, there are some functions that use regex to find any match in the string. Let's see some examples of using regex patterns
Method | Description |
---|---|
exec() | A RegExp the method that executes a search for a match in a string. It returns an array of information. |
test() | A RegExp the method that tests for a match in a string. It returns true or false. |
match() | A String the method that executes a search for a match in a string. It returns an array of information or null on a mismatch. |
search() | A String the method that tests for a match in a string. It returns the index of the match, or -1 if the search fails. |
replace() | A String the method that executes a search for a match in a string, and replaces the matched substring with a replacement substring. |
split() | A String the method that uses a regular expression or a fixed string to break a string into an array of substrings. |
String search()
A String method tests for a match in a string. It returns the index of the match, or -1 if the search fails.
<!DOCTYPE html>
<html>
<body>
<p id="element"></p>
<script>
var str = "Hello This is www.Web.com";
document.getElementById("element").innerHTML = str.search(/Web/i);
</script>
</body>
</html>
String replace()
A String method executes a search for a match in a string and replaces the matched substring with a replacement substring.
<!DOCTYPE html>
<html>
<body>
<p id="element"></p>
<script>
var str = "Hello This is www.Web.com";
document.getElementById("element").innerHTML = str.replace(/web/i,"Web");
</script>
</body>
</html>
Regular Expression Modifiers
Regular expressions have four optional flags that allow for global and case insensitive searching. These flags can be used separately or together in any order and are included as part of the regular expression.
Flag | Description |
---|---|
g | Global search. |
i | Case-insensitive search. |
m | Multi-line search. |
y | Perform a "sticky" search that matches starting at the current position in the target string. |
Regular Expression Patterns
Simple patterns are constructed of characters for which you want to find a direct match. For example, the pattern /ABC/ matches character combinations in strings only when exactly the characters 'ABC' occur together and in that order. Such a match would succeed in the strings "Hi, do you know your ABC's?" and "The latest airplane designs evolved from slabcraft." In both cases, the match is with the substring 'ABC'. There is no match in the string 'Grab crab' because while it contains the substring 'ABC', it does not contain the exact substring 'ABC'.
Expression | Description |
---|---|
[abc] | Find any of the characters between the brackets |
[0-9] | Find any of the digits between the brackets |
(x|y) | Find any of the alternatives separated with | |
Metacharacter | Description |
---|---|
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning or at the end of a word |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
Quantifier | Description |
---|---|
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 occurrence of n |
Test()
Let's talk a little about test function. A RegExp method tests for a match in a string. It returns true or false. Use test() whenever you want to know whether a pattern is found in a string
<!DOCTYPE html>
<html>
<body>
<p id="element"></p>
<script>
var reg = /web/i;
var str = "Hello This is www.web.com";
document.getElementById("element").innerHTML = reg.test(str);
</script>
</body>
</html>
Exec()
Here is another regular expression function. A RegExp method that executes a search for a match in a string. It returns an array of information.
<!DOCTYPE html>
<html>
<body>
<p id="element"></p>
<script>
var reg = /Web/i;
var str = "Hello This is www.web.com";
document.getElementById("element").innerHTML = reg.exec(str);
</script>
</body>
</html>
0 Comments
CAN FEEDBACK
Emoji