The following is only as a personal summary, if wrong, please also point out, thank you
The use of regular expressions is broadly divided into three types:
- Determine if a string matches a pattern
- Replace the content in a string that conforms to a pattern
- extracts the contents of a string that conform to a pattern
JS Create regular expression:
- By instantiating a class
New RegExp (patternattributes);
Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression
The parameter attributes is an optional string, called a modifier, that contains the attribute "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.
Note that if pattern is not a valid regular expression, or attributes contains characters other than "G", "I", and "M", the exception hint SyntaxError is thrown; if pattern is a RegExp object, but does not omit the attributes parameter and throws the exception hint TypeError
-
The literal Way
/pattern/attributes
JS Regular expression-related properties:
- Global identifies whether the regular has a modifier g
- IgnoreCase identifies if the regular has a modifier i
- Multiline identifies whether the regular has a modifier m
- String literals in the source regular expression
- LastIndex An integer that marks the beginning of the next matching character position (the value is modified after the test and Exec methods are executed for the next match)
JS Regular Expression Correlation method:
- The Compile method is
pattern converted to an internal format, which executes faster. For example, this allows you to use regular expressions more efficiently in loops. When the same expression is reused, the compiled regular expression makes the execution faster. However, this compilation is useless if the regular expression changes.
- Test retrieves whether the string matches the regular. The match returns True otherwise false is returned.
- EXEC retrieves the value specified in the string. Returns the found value and determines its location. If the regular expression contains a capturing type grouping, the returned value also contains the captured result
Methods for String objects that support regular expressions
- Search
- Match
- Replace
- Split
Follow-up content to be perfected ...
Beginner JS Regular