Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of regular expressions in JavaScript programming_Basic knowledge

Detailed explanation of the use of regular expressions in JavaScript programming_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:35:021342browse

RegExp: is the abbreviation of regular expression.
What is RegExp?
Regular expressions describe character pattern objects.
When you retrieve some text, you use a pattern to describe what you want to retrieve. RegExp is this pattern.
Simple patterns can be a single character.
More complex patterns include more characters and can be used for parsing, format checking, substitution, etc.
You can specify where in the string to search, what type of characters to search for, and so on.
Grammar

var patt=new RegExp(pattern,modifiers);

or

var patt=/pattern/modifiers;

A pattern describes an expression model.
Modifiers describe whether the search is global, case-sensitive, etc.
RegExp modifier
The
modifier is used to perform case-insensitive and full-text searches.

  • i - modifier is used to perform case-insensitive matching.
  • g - The modifier is used to perform a full-text search (instead of stopping at the first one found, find all matches).

Example 1

Look for "W3CSchool" in the string without case sensitivity

var str="Visit W3CSchool";
var patt1=/w3cschool/i;

The following marked text is the obtained matched expression:

Visit W3CSchool

Example 2
Full text search "is"

var str="Is this all there is?";
var patt1=/is/g;

The following marked text is the obtained matched expression:

Is this all there is?


Example 3
Full-text search and case-insensitive search for "is"

var str="Is this all there is?";
var patt1=/is/gi;

The following marked text is the obtained matched expression:

Is this all there is?

Let’s take a look at some of the basic regular expression objects available in JS:
Modifier
Modifiers used to perform case-sensitive and global matching:

20151025161547878.png (747×129)

Square brackets
Square brackets are used to find a range of characters:

20151025161800522.png (741×318)

Metacharacters
Metacharacters are characters with special meanings:

20151025161818741.png (739×595)

Quantifier

20151025161837302.png (744×344)

RegExp object method

20151025161901940.png (744×129)

Methods of String objects that support regular expressions

20151025161917774.png (743×162)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn