Regular Expression and RegExp in JavaScript

A regular expression is an object that describes a pattern of characters.

A regular expression is an object that describes a pattern of characters.

The JavaScript RegExp class represents Regular Expressions, and both String and RegExp define methods that use Regular Expressions to perform strong pattern matching and search-replace functions on text.

Syntax

A Regular Expression can be defined with the RegExp () constructor as follows:

 var  pattern  = new RegExp ( pattern ,  attributes ); or  simply  var  pattern  = /pattern/ attributes ; 

Description of parameters:

  • pattern - A string that specifies the pattern of another regular expression or regular expression.
  • attributes - An optional string containing any "g", "i", and "m" attributes that define Global, not case-insensitive, and multiple matches (multiline matches).

Square brackets

Square brackets ([]) have a special meaning when used in Regular Expression. They are used to find a sequence of characters.

Expression Description [.] Any one of the characters in square brackets [^ .] Any character not enclosed in square brackets [0-9] It matches any decimal number from 0 to 9 [az] It matches any lowercase character from a to z. [AZ] It matches any uppercase character from A to Z. [aZ] It matches any character from lowercase a to uppercase Z.

The ranges shown above are common; You can also use the range [0-3] to match any decimal from 0 to 3, or the range [bv] to match any lowercase character from b to v .

Quantifier

The frequency and position of character sequences and single characters enclosed in brackets can be indicated by a special character. Each special character has a separate extension. The +, * . follow a sequence of characters.

Expression Description + It matches any string containing at least one p. p * It matches any string containing 0 or more p. p? It matches any string containing 1 or more pp { N } It matches any string containing a sequence with { N } pp {2,3} It matches any string containing a sequence of 2 or 3 pp {2,} It matches any string containing a sequence of at least 2 pp $ It matches any string ending with p ^ p It matches any string starting with p

For example

The following example explains more details about matching characters.

Expression Description [^ a-zA-Z] It matches any string that does not contain any characters from a to z and from A to Z. pp It matches any string containing p , followed by any character, followed by another p character. ^. {2} $ It matches any string that contains exactly 2 characters (. *) It matches any string in the tag and . p (hp) * It matches any string containing p followed by 0 or more ranges hp .

Literal characters

Character DescriptionAlphanumeric itself Character NUL (u0000) t Tab (u0009) n New line (u000A) v Vertical tab (u000B) f Form feed (u000C) r Carriage return (u000D) xnn Latin characters are defined by decimal numbers hexagon nn; for example, x0A is like nxxxx Unicode character is defined by xxxx hexadecimal number; for example: u0009 is t cX Control character ^ X; For example, cJ is equivalent to a new line character n

Metacharacter (Metacharacter)

A metacharacter is simply an alphabetic character preceded by a backslash that performs a special meaning match.

For example, you can search for a large amount of money by using super characters 'd' as: / ([d] +) 000 / . Here, d will find any numeric string.

The following table lists a set of metacharacters that can be used in PERL Style Regular Expression.

Character Description. A single character s A blank space character (new space, tab, line) S Not a blank space character d A digit (0-9) D Not a digit w A word character (az, AZ , 0-9, _) W Not a word from [b] A literal backspace (special case) [aeiou] Matches a single character in a given set [^ aeiou] Matches a single character outside the given set (foo | bar | baz) Matches any given change sequence

Quantify (Modifier)

Some quantifiers are available that can determine how you work with regexp , like typography, multi-line search, etc.

Modifier Load execution Matches regardless of case-insensitive. Determine if the string has newline characters (new line) or carriage return, operator ^ and $ will match a newline limit (new line), instead of a string limit. g Perform Global matching, which finds all matches instead of stopping after the first match.

Properties of RegExp

The following table lists the properties of the RegExp object and describes:

Attribute Descriptionconstructor Define the function to create the prototype of the object. global Specifies if the "g" quantifier is set to ignoreCase Specifies if the "i" quantifier is set to lastIndex Index at which the new multiline match is determined Specify if the "m" quantifier is set to Text source of pattern

RegExp methods

The following table lists the methods of the RegExp object and describes:

Method Descriptionexec () Executes a search for a match in its string parameter. test () Check a match in its string parameter. toSource () Returns a literal object representing the given object; You can use this value to create a new object. toString () Returns a string representing the given object.

According to Tutorialspoint

Previous lesson: Math object in JavaScript

Next lesson: Document Object Model (DOM) in JavaScript

Thanks for reading the post

Regular Expression and RegExp in JavaScript

Please share this article to your friends
« Document Object Model (DOM) in JavaScript
Math object in JavaScript »