In this chapter we will learn about Regexp.
- literal way
- Constructor function way
Let's see how we can use those ways.
Using a regular expression literal, as follows:
Calling the constructor function of the RegExp object, as follows:
var variable = new RegExp(patern);
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 which uses regex to find any match in the string. Let's see some examples of using regex patterns
A String method tests for a match in a string. It returns the index of the match, or -1 if the search fails.
<html> <body> <p id="element"></p> <script> var str = "Hello This is www.w3docs.com"; document.getElementById("element").innerHTML = str.search(/w3docs/i); </script> </body> </html>
A String method executes a search for a match in a string and replaces the matched substring with a replacement substring.
<html> <body> <p id="element"></p> <script> var str = "Hello This is www.w3docs.com"; document.getElementById("element").innerHTML = str.replace(/w3docs/i,"W3DOCS"); </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.
|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'.
|[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 ||
|\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|
|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|
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
<html> <body> <p id="element"></p> <script> var reg = /w3docs/i; var str = "Hello This is www.w3docs.com"; document.getElementById("element").innerHTML = reg.test(str); </script> </body> </html>
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.
<html> <body> <p id="element"></p> <script> var reg = /w3docs/i; var str = "Hello This is www.w3docs.com"; document.getElementById("element").innerHTML = reg.exec(str); </script> </body> </html>