This website uses cookies

We use cookies to personalize content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners who may combine it with other information that you've provided to them or that they've collected from your use of their services. Please read more at our privacy policy page.



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 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 which uses regex to find any match in the string. Let's see some examples of using regex patterns

Method Description
exec() A RegExp method that executes a search for a match in a string. It returns an array of information.
test() A RegExp method that tests for a match in a string. It returns true or false.
match() A String 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 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 method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.
split() A String method that uses a regular expression or a fixed string to break a string into an array of substrings.

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.w3docs.com";
      document.getElementById("element").innerHTML = str.search(/w3docs/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.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.

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 occurrences 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 = /w3docs/i;
      var str = "Hello This is www.w3docs.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 = /w3docs/i;
      var str = "Hello This is www.w3docs.com";
      document.getElementById("element").innerHTML = reg.exec(str);
    </script>
</body>
</html>