A regular expression of JS
1. What is a regular expression
Regular Expressions (Regular expression) is an object that describes the character pattern, the RegExp class of ECMAScript represents regular expressions, while String and RegExp All define powerful pattern-matching and text-retrieval and substitution functions using regular expressions.
2. How to create a regular expression:
2.1 Way 1:new Way created
var box = new RegExp (' box ', ' IG ');
2.2 Way 2: Create a literal way
var box =/box/ig;
2.3 The first parameter represents the matched string, the second parameter represents the regular match, and the main three match types are:
I: Ignores the size of the matched string.
G: The matched string is globally matched.
M: The matched string is matched in multiple lines.
3, the regular expression matching method:
3.1 Method 1:new () method
3.1.1 Example of the test method using the new operator :
var pattern = new RegExp (' box ', ' I '); Create regular mode, case insensitive
var str = ' This is a box! '; Create a string to compare to
Alert (Pattern.test (str)); the test () method verifies whether a match is returned with a Boolean value of TRUE or Flase
3.1.2 Example of the test method using the literal method :
var pattern =/box/i; Create regular mode, case insensitive
var str = ' This is a box! ';
Alert (Pattern.test (str));
3.1.3 using a single statement for regular matching
Alert (/box/i.test (' This is a box! ')); pattern and string replaced two variables
3.2 Method 2:exec () method
The 3.2.1 method is similar to test ().
var pattern =/box/i; Create regular mode, case insensitive
var str = ' This is a box! '; Create a string to compare to
Alert (pattern.exec (str)); The returned array is matched, otherwise null is returned
JavaScript Advanced Syntax III