What is a regular expression?

A regular expression is a search pattern formed by a sequence of characters. When you search for data in text, you can use search patterns to describe what you are looking for; A regular expression can be a simple character or a more complex pattern. Can be used for all text search and text replacement operations.

Grammar:

/ Regular expression body/modifier (optional)Copy the code

Ex. :

var patt = /runoob/i
Copy the code
  • /runoob/ I is a regular expression.
  • Runoob is a regular expression body (for retrieval).
  • I is a modifier (search is case insensitive).

Common regular expression symbols

symbol instructions
literal Matches the value of the string
. Matches any character except newline \n
^ Matches the beginning of the string
$ Matches the end of the string
[…]. Matches any character in []
[x-y] Matches any character from character x to character y
[^…]. Does not match any character present in this character set
* Matches the previous regular expression zero or more times
+ Matches the previous regular expression one or more times
? Matches the previous regular expression zero or once
{N} Matches the previous regular expression N times
{M,N} Matches the previous regular expression M to N times
(a) Matches the regular expression in () and saves it as a subgroup

Special characters

Special characters instructions
\d The same as the [0-9]
\D The same as the [^ 0-9]
\w And [A Za – z0-9] the same
\W And [^ A – Za – z0-9] the same
\s Matches any whitespace character, same as [\n\t\r\v\f]
\S Same as [^ \n\t\r\v\f]

Regular expression modifier

The modifier describe
i Performs case-insensitive matching
g Perform global matches (find all matches instead of stopping after finding the first match)
m Perform multi-line matching

Using string methods In JavaScript, regular expressions are typically used for two string methods: search() and replace().

  • The search() method is used to retrieve a substring specified in a string, or a substring that matches a regular expression, and return the starting position of the substring.
  • The replace() method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.

The search() method uses regular expressions

Example: Search the string “Runoob” with a regular expression, case insensitive:

var str = "Visit Runoob!"; 
var n = str.search(/Runoob/i);
Copy the code

The search() method takes a string. String arguments are converted to regular expressions:

Example: Retrieve a substring of “Runoob” in a string:

var str = "Visit Runoob!"; 
var n = str.search("Runoob");
Copy the code

The replace() method uses regular expressions

Example: Replace Microsoft with Runoob in a string using a regular expression and being case insensitive:

var str = document.getElementById("demo").innerHTML; 
var txt = str.replace(/microsoft/i."Runoob");
Copy the code

The replace() method uses a string

Example: The replace() method accepts a string as an argument:

var str = document.getElementById("demo").innerHTML; 
var txt = str.replace("Microsoft"."Runoob");
Copy the code