This article mainly introduces how to use regular expressions in JavaScript programming and lists the oxo Regular Expressions supported by JS. For more information, see RegExp: it is short for a regular expression (regular expression.
What is RegExp?
Regular expressions describe character pattern objects.
When you retrieve a text, you can use a mode to describe the content to be retrieved. RegExp is in this mode.
A simple mode can be a single character.
More complex modes include more characters and can be used for parsing, format check, replacement, and so on.
You can specify the search position in the string and the type of characters to be retrieved.
Syntax
var patt=new RegExp(pattern,modifiers);
Or
var patt=/pattern/modifiers;
Mode describes an expression model.
Modifier describes whether the search is global and case sensitive.
RegExp Modifier
Modifiers are used to perform case-insensitive and full-text searches.
- The I-modifier is used to perform case-insensitive matching.
- The g-modifier is used to perform a full-text search (instead of stopping the search when the first one is found, but finding all the matches ).
Instance 1
Find "W3CSchool" in strings case-insensitive"
var str="Visit W3CSchool";var patt1=/w3cschool/i;
The text marked below is a matched expression:
Visit W3CSchool
Instance 2
Full-text search "is"
var str="Is this all there is?";var patt1=/is/g;
The text marked below is a matched expression:
Is this all there is?
Instance 3
Full-text search and case-insensitive search "is"
var str="Is this all there is?";var patt1=/is/gi;
The text marked below is a matched expression:
Is this all there is?
Next, let's take a look at some of the basic regular expression objects available in JS:
Modifier
The modifier is used for case-sensitive and global matching:
Square brackets
Square brackets are used to find characters in a certain range:
Metacharacters
Metacharacter is a character with special meanings:
Quantifiers
RegExp object Method
Methods for String objects that support regular expressions