Regular Expression and RegExp in JavaScript

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:

  1. pattern - A string that specifies the pattern of another regular expression or regular expression.
  2. 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

4.5 ★ | 2 Vote

May be interested

  • Syntax of JavaScriptSyntax of JavaScript
    javascript can be implemented using javascript commands that are placed in html tags ... in a web page.
  • What is JavaScript? Can the Internet exist without JavaScript?What is JavaScript?  Can the Internet exist without JavaScript?
    not everyone knows what javascript is and how it works. the long and fascinating development history of javascript as well as what we can do with javascript is still unknown.
  • What is Currying in Javascript? How to use Currying in JavaScriptWhat is Currying in Javascript? How to use Currying in JavaScript
    the currying feature in javascript can help you keep your code tidy and give you a new way of seeing how functions work. currying is ideal when you want to break complex logic into smaller, manageable, and self-contained pieces of code.
  • Things to know about 'this' in JavaScriptThings to know about 'this' in JavaScript
    are you having trouble understanding the keyword 'this' in javascript ? then please read what you need to know about 'this' in javascript below.
  • Udemy's top 5 JavaScript coursesUdemy's top 5 JavaScript courses
    a programming language that runs on any computer in the world. a language does not need any special software to run. a language ranked among the top in the world.
  • Top site with many good JavaScript exercises to practiceTop site with many good JavaScript exercises to practice
    many people, after taking javascript courses and being equipped with some knowledge of the language they pursue, are eager to improve and cultivate these learned skills. so this article will give you a list of the top 3 websites to actually make javascript.
  • What is JavaScript?What is JavaScript?
    javascript is a programming language of html and web. it is lightweight and most commonly used as part of web pages, but their implementation allows client-side scripts to interact with users and create dynamic websites. it is an interpreted programming language with object-oriented capabilities.
  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • Learn about ES6 in JavascriptLearn about ES6 in Javascript
    es6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
  • Summary of JavaScript exercises with sample codeSummary of JavaScript exercises with sample code
    in order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.