Javascript RegExp Object

In Javascript RegExp constructor is used to match character with a pattern by creating a regular expression object. The regular expression's elements specify what you would like to match or replace. These patterns are used with the 'exec' and 'test' methods of RegExp, and with the match, replace, search, and split methods of String.

Syntax

/pattern/flags new RegExp(pattern[, flags])

pattern : The text of the regular expression.

flags : Flags is optional but if you specified, flags can have any combination of the following values:

Regular Expression Flags

Flags Description
i This is used for ignore case
g This is used for global match
m This is used for multiline
y This is used to sticky; matches only

Example

There are two ways to create a RegExp object: a literal notation and a constructor.

<html>
<head>
<title>Javascript RegExp Object</title>
<SCRIPT LANGUAGE="JavaScript">
var str_pat = /techStrikers/i //literal notation
var re = new RegExp('some string', 'i'); //constructor
</head>
<body>
</body>
</html>

Regular Expression Patterns

Expression Description
[xyz] Any one character x or y or z, as listed in square brackets
[x-z] Any one character within the range specified in brackets
[^xyz] Any one character except x, y, z
[^x-z] Any one character except the range in brackets
. Any character except newline
\d \D Any digit (\d), any non-digit (\D)
\s \S Any whitespace (\s), any non-whitespace (\S)
\w \W Any word character (\w), any non-word character (\W)
\b \B Word boundary (\b), non-boundary (\B)
[\b] Backspace (the square brackets help distinguish backspace from word boundary)
^ Beginning of string; beginning of line if the m flag is set
$ End of string; end of line if the m flag is set
\n \r \f \t \v Newline, carriage return, form feed, tab, vertical tab
\+ \- \. \* \? \| Match + - . * ? | etc. verbatim (not as a special character)
\/ \\ \^ \$ Match / \ ^ $ verbatim (not as a special character)
\[ \] \( \) \{ \} Match brackets/parentheses/braces verbatim
\xAB Match the character with hexadecimal code AB
\uABCD Match the Unicode character with hexadecimal code ABCD
x|y Match x or y
+ Match the preceding element one or more times
* Match the preceding element zero or more times
? Match the preceding element zero or more times
{n} Match the preceding element exactly n times
{n,} Match the preceding element exactly n times
{m,n} Match the preceding element at least m, at most n times
(...) Match ... as a capturing group: store the matching substring
\1 \2 \3 etc. Match the same substring as in capturing group number 1, 2, 3 etc.
$1 $2 $3 etc. Replace with the characters that matched group number 1, 2, 3 etc.
(?:...) Match ... as a non-capturing group: grouping only, no storing

Regexp Methods:

Here is a list of each method of regexp object with description.

MethodDescription
test()This method search string for a given pattern.
exec()This method search for a match in a specified string

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.