regular font:Detailed description of regular expression (1) Implementation method-Font Tutorial免费ppt模版下载-道格办公

Detailed description of regular expression (1) Implementation method

regular expression is a regular expression, it seems that English is much easier to understand than Chinese, just check that the expression
does not meet the regulations! ! Regular expressions have a very powerful and complex object RegExp, which is provided in JavaScript1.2 and above.
Let's take a look at the introduction of regular expressions:
The regular expression object is used to standardize a standardized expression (that is, the expression character does not meet specific requirements, such as whether it is an Email
address format etc.), which has properties and methods for checking whether a given string conforms to the rules.
In addition, the properties of individual regular expression objects that you create with the RegExp constructor have pre-defined the static properties of regular expression
objects, and you can use them at any time.
Core Objects:
Provided in javascript 1.2, NES 3.0 and above.
The toSource method is added in versions after JavaScript 1.3.
Create method:
text format or RegExp constructor function.
Text creation format uses the following format:
/pattern/flags ie /pattern/flag

The constructor function method is used as follows:
new RegExp("pattern" [, "flags"]) ie new RegExp("pattern" [,"mark"])

Parameter:
pattern(mode)
Represents the regular expression text

flags(tag)
If this is specified, flags can be the following values One:
g: global match (full match)
i: ignore case (ignore case)
gi: both global match and ignore case (match all possible values, also ignore case)

Note: Do not use quotation marks for parameters in text format, and for constructor functions Parameters are marked with quotes. So the following
expression creates the same regular expression:
/ab+c/i
new RegExp("ab+c", "i")

Description:
When using the constructor, you must use normal characters String escaping rules (adding leading characters to strings) are required.
For example, the following two statements are equivalent:
re = new RegExp(“\w+”)
re = /w+/

The following provides a complete list of special characters that can be used in regular expressions A full list and description.

Table 1.3: Special characters in regular expressions:

Character
Meaning: For characters, it usually means literal meaning, indicating that the next The characters in are special characters and will not be interpreted.
For example: /b/ matches the character 'b', by adding a backslash in front of b, that is, /b/, the character becomes a special character, which means that
matches the boundary of a word.
Or:
For several characters, it is usually specified as special, indicating that the following characters are not special, but should be interpreted literally.
For example: * is a special character that matches any character (including 0 characters); for example: /a*/ means to match 0 or more a.
In order to match literal *, add a backslash in front of a; for example: /a*/ matches 'a*'.

Character^
Meaning: Indicates that the matching character must be at the front.
For example: /^A/ does not match the 'A' in "an A," but matches the first 'A' in "An A.".

Character $
Meaning: similar to ^, matching the last character.
For example: /t$/ does not match the 't' in "eater", but matches the 't' in "eat".

Character *
Meaning: match * the previous character 0 or n times .
Example: /bo*/ matches 'boooo' in "A ghost booooed" or 'b' in "A bird warbled", but not any character in "A goat g
runted".

Character +
Meaning: Match the character before the + sign once or n Second-rate. Equivalent to {1,}.
For example: /a+/ matches 'a' in "candy" and all 'a' in "caaaaaaandy."

Character?
Meaning: Match the character before? 0 or 1 time .
Example: /e?le?/ matches 'el' in "angel" and 'le' in "angle."

Character.
Meaning: (decimal point) matches all characters except line breaks single character.
For example: /.n/ matches 'an' and 'on' in "nay, an apple is on the tree", but not 'nay'.

character (x)
meaning: match 'x' and record the matching value.
Example: /(foo)/ matches and records 'foo' in "foo bar.". Matching substrings can be returned by elements [1], …, [n] in the result array
, or by properties $1, …, $9 of the RegExp object.

Character x|y
Meaning: match 'x' or 'y' .
For example: /green|red/ matches 'green' in "green apple" and 'red' in "red apple."

Character {n}
Meaning: Here n is a positive integer. Matches the preceding n characters.
For example: /a{2}/ does not match the 'a' in "candy," but matches all the 'a' in "caandy," and the first two
'a in "caaandy." '.

Character {n,}
Meaning: here n is a positive integer . Matches at least n of the preceding characters.
For example: /a{2,} does not match 'a' in "candy", but matches all 'a' in "caandy" and all 'a' in "caaaaaaandy."

Character {n,m}
Meaning: Both n and m here is a positive integer. Matches at least n and at most m of the preceding characters.
For example: /a{1,3}/ does not match any character in "cndy", but matches 'a' in "candy," and the first two in "caandy,"
'a' And the first three 'a' in "caaaaaaandy", note: even if there are many 'a' in "caaaaaaandy", it only matches the first three
'a', namely "aaa".

Character [xyz]
Meaning: a list of characters, matching the list any of the characters. You can indicate a range of characters with the hyphen -.
For example: [abcd] is the same as [a-c]. They match 'b' in "brisket" and 'c' in "ache".

Character [^xyz]
Meaning: a character complement, that is Say, it matches everything except the listed characters. You can use the hyphen - to indicate a range of
characters.
For example: [^abc] and [^a-c] are equivalent, they match 'r' in "brisket" and 'h' in "chop." at the earliest.

Character [b]
Meaning: match a space (not to be confused with b )

Character b
Meaning: match the dividing line of a word, such as a space (Not to be confused with [b])
Example: /bnw/ matches 'no' in "noonday", /wyb/ matches 'ly' in "possibly yesterday."

Character B
Meaning: match a non-demarcation line of a word
For example: /wBn/ matches 'on' in "noonday", /yBw/ matches 'ye' in "possibly yesterday."

Character cX
Meaning: Here X is a control character. Matches a string of control characters.
For example: /cM/ matches control-M in a string.

Character d
Meaning: match a number, equivalent to [0- 9].
For example: /d/ or /[0-9]/ matches '2' in "B2 is the suite number."

Character D
Meaning: match any non-number, equivalent to [ ^0-9].
For example: /D/ or /[^0-9]/ matches 'B' in "B2 is the suite number."

Character f
Meaning: match a form character

Character n
Meaning: match a newline

Character r
Meaning: match a carriage return

Character s
Meaning: match a single white space character, including spaces, tab, form feed, line break, equivalent to [fnrtv].
Example: /sw*/ matches 'bar' in "foo bar."

Character S
Meaning: match a single character except white space character character, equivalent to [^fnrtv].
Example: /S/w* matches 'foo' in "foo bar."

Character t
Meaning: match a tab

Character v
Meaning: match a head tab

Character w
Meaning: match all numbers and letters and underscores, etc. Valence at [A-Za-z0-9_].
Example: /w/ matches the 'a' in "apple," the '5' in "$5.28," and the '3' in "3D."

Character W
Meaning: match except numbers, letters and underscores Other characters, equivalent to [^A-Za-z0-9_].
For example: /W/ or /[^$A-Za-z0-9_]/ matches the '%' in "50%.".

Character n
Meaning: Here n is a positive integer. Matches the value of n (counting opening parentheses) of the last substring of a regular expression.

For example: /apple(,)sorange1/ matches "apple, orange, cherry, peach.", there is a more complete example below
.
Note: If the number in the left parenthesis is smaller than the number specified by n, then n takes the octal escape of the next line as the description.

The characters ooctal and xhex
Meaning: The ooctal here is an octal escape value, and xhex is a hexadecimal escape value that allows embedding ASCII codes within a
regex.

While the expression is checked, the text notation provides the ability to edit the regular expression method. The regular expression
can be kept as a constant by using literal symbols. For example, if you use literal symbols in a loop to construct a regular expression, the regular expression doesn't have to be
compiled repeatedly.
Regular expression object constructors, for example, new RegExp("ab+c"), provide runtime compilation of regular expressions. Constructors should be used when you know that the pattern of the regex will change, or you don't know the pattern of the regex and they are obtained from another source, such as input by the user hour. Once you define a regular expression, the regular expression can be used anywhere
and can be changed, you can use the compile method to compile a new regular expression for reuse.
A separate predefined RegExp object can be used in each window; that is, each separate JavaScript thread running
gets its own RegExp object. Because each script is uninterruptible within a thread, this ensures that different scripts will not overwrite the value of the RegExp object.
The predefined RegExp object contains static attributes: input, multiline, lastMatch, lastParen, leftContext,
rightcontext, and from $1 to $9. The input and multiline attributes can be preset. The values ​​of other static properties are set after executing the exec and test methods of individual regular expression objects, and after executing the match and replace methods of strings.

Attributes
Note that several attributes of the RegExp object have both long names and Short name (like perl). These names all point to the same value. Perl is a programming language, and JavaScript mimics its regular expressions.

Attributes $1, …, $9
Get matching substring, if any words

Attribute $_
reference input

Attribute $*
refer to multiline

Attribute
refer to lastMatch

attribute $+
refer to lastParen

AttributeRegular expression regular expression details (1) Implementation method- Today's headlines< link rel="canonical" href="https://www.toutiao.com/article/7232941782525526531/">