What is the role of special characters in JavaScript Regular Expressions?


The frequency or position of bracketed character sequences and single characters can be denoted by a special character. Each special character has a specific connotation. The +, *, ?, and $ flags all follow a character sequence.

Sr.No
Expression & Description
1
p+
It matches any string containing one or more p's.
2
p*
It matches any string containing zero or more p's.
3
p?
It matches any string containing at most one p.
4
p{N}
It matches any string containing a sequence of N p's
5
p{2,3}
It matches any string containing a sequence of two or three p's.
6
p{2, }
It matches any string containing a sequence of at least two p's.
7
p$
It matches any string with p at the end of it.
8
^p
It matches any string with p at the beginning of it.

Example

You can try to run the following code to learn how to work with special characters in JavaScript Regular Expressions −

<html>
   <head>
      <title>JavaScript Regular Expressions</title>
   </head>
   <body>
      <script>
         var myStr = "Welcome to our website! Welcome to Tutorialspoint!";
         var reg = /Wel*/g;

         var match = myStr.match(reg);

         document.write(match);
      </script>
   </body>
</html>

Updated on: 23-Jun-2020

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements