Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 15:35:02
Original
1361 people have browsed it

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);
Copy after login

or

var patt=/pattern/modifiers;
Copy after login

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;
Copy after login

The following marked text is the obtained matched expression:

Visit W3CSchool
Copy after login

Example 2
Full text search "is"

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

The following marked text is the obtained matched expression:

Is this all there is?
Copy after login


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

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

The following marked text is the obtained matched expression:

Is this all there is?
Copy after login

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)

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template