/* Test environment: Chrome 63.0.3239.132 */
The optional value of the regular object modifier in JS is: "i" "g" "m", that is, ignore case and perform global matching. Multi-line mode
Metachars supported by regular expressions in JS:
1: Enumerate square bracket expressions, range square bracket expressions, enumerate the inverse square bracket expressions, range the inverse square bracket expressions
2:. \w \W \d \D \s \S \b \B \n \r \f \v \888 \uffff \xff
3: n? n+ n* n{a} n{a,} n{a,b} and the non-greedy pattern of these quantifiers
4:^ $ (?!) (?=) (?:)
other:
1: Please use \number to refer to grouping in JS. Named groups such as '' and <> are not supported.
2: The possession mode that does not support quantifiers
3: Reverse ring view is not supported
4: The composite expressions in square bracket expressions support the following:
[abc0-9] [abc\d] [\d\w^&] and so on
5: Support modifier combinations, such as gim mig gi ig, etc., no order requirements
JS regular object properties:
global: Whether global matching is enabled, a boolean value
ignoreCase: Whether the object is turned on ignore case, a boolean value
multiline: Whether the object is enabled for multi-line mode, a boolean value
lastIndex: The position where the object starts to match next time, that is, the position where the last match ends, a positive integer
source: The source text of the regular expression, that is, the regexp of /regexp/flag, does not contain modifiers, a string
Create a regular expression:
1:myreg = new RegExp(patternString[,globalFlags]);
2:myreg = /yourRegexp/globalFlags;
Common methods of regular expressions in JS:
In the RegExp object:
1:test(yourString);
Definition: Detect whether a string matches a pattern.
Other notes: This method will ignore the "g" flag and the lastIndex attribute of the regular object (that is, it is always found from the beginning), and the match will be successful as long as you find a string that matches the pattern.
2:exec(yourString);
Definition: Retrieves matches of regular expressions in strings.
Other instructions: If the "g" flag is not enabled, only search once, and do not modify the lastIndex property of the regular object, return an object, including the subscripts 0, index, and input. At this time, the attribute is equivalent to match; if "g" is enabled, the lastIndex property of the regular object is modified to the end of the substring found this time, and return an object that is the same as "g" not enabled. (You can modify the lastIndex property of the regular object by yourself to specify where the exec starts to search.)
In a String object:
1:search(yourRegexpOrString);
Definition: Searches a substring in a string that matches the specified substring or regular expression.
Other description: This method will ignore the "g" flag and the lastIndex property of the regular object (that is, it is always found from the beginning), return the position of the first character found, and return -1 if not found.
2:match(yourRegexpOrString);
Definition: Find one or more strings that match the regular expression within the string and return an object. (Ignore the laseIndex property)
Other instructions: If the "g" flag is not turned on, only the first matching string will be found, and an object will be returned, including subscripts 0, index, and input, where subscript 0 is equivalent to index, input is a reference to String; if "g" is turned on, and an array will be returned, the length of the array is the number of matching strings, and each element is the starting character position of each match.
3:replace(yourRegexpOrString,placementString);
Definition: Used to replace other characters in a string with some characters, or replace a substring that matches a regular expression.
Other notes: If there is no "g" flag, it will only be replaced once, and if the argument is a string instead of a regular object, it will always be replaced once. Returns a string, which is the result after replacing yourRegexpOrString with placementString. $ has special uses in placementString, see the following table:
$1、$2、...、$99 | Grouping of references captured in regexp. |
$& | The text matched this time. |
$` | The text on the left side of the substring matched this time. |
$' | The text on the right side of the substring matched this time. |
$$ | $ itself. |
The second parameter of this method can be a function. The function will be called every time it matches, and the return value of the function is used as the placement. A total of 4 parameters are passed in. The first parameter is the subtext ($&) of this match, the intermediate parameter is the subexpression matching string, the number is unlimited ($i). The penultimate parameter is the subscript position of the subtext matched to this match, and the last parameter represents the string itself that executes the place method.
4:split(yourRegexpOrString[,howMany]);
Definition: Split a string into an array of string values.
Other description: Yes (separatorString) reverse operation. If "" is passed in, each character will be separated into an array. howMany specifies the number of returned arrays.
Summarize
The above is a summary of JavaScript regular expression functions introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!