JavaScript regular expression (1)

xiaoxiao2021-03-05  23

Regular Expression objects contain a regular expression mode (PATTERN). It has regular expression

Modular mode to match or replace the properties of a specific character (or character set) in a string (String)

Methods. To add an attribute for a separate regular expression, you can use a regular expression constructor

(constructor function), whenever the pre-set regular expression is static attribute (the

Predefined Regexp Object Has Static Properties That Are Set WHENEVER ANY

Regular Expression IS Used, I don't know if I turn it wrong, list the original text, please translate it yourself).

create:

A text format or regular expression constructor

Text format: / pattern / flags

Regular expression constructor: new regexp ("pattern" [, "flags"]);

Parameter Description:

Pattern - a regular expression text

Flags - If there is, it will be the following value:

g: Global match

i: ignore the case

GI: above combination

[Note] The parameters of the text format do not need quotation marks, and quotation marks when using the constructor. Such as: / ab c / I new

Regexp ("AB C", "I") is the same functionality. In the constructor, some special characters need to be converted (in special

Special characters before "/"). Such as: re = new regexp ("// w ")

Special characters in regular expressions

Character content /

As a transaction, ie the characters usually in "/" do not explain it as original, such as / b / match character "B", when B is added to the front of the back rod /

/ B /, transliterate to match the boundary of a word.

-or-

Restore of the regular expression function character, such as "*" matches its front metamorphic characters 0 times or more, / a * / will match A, AA, AAA, plus

After "/", / A / * / will only match "a *".

^ Match an input or row, / ^ A / match "AN A", without matching "an a" $ matching an input or one line

Tail, / a $ / match "an a" without matching "an a" * matches the front metamorphic characters 0 times or multiple times, / ba * / will match

B, Ba, BAA, Baaa match front character characters 1 or more, / ba * / will match BA, BAA, Baaa? match front character 0

Time or 1 time, / ba * / will match B, Ba (x) match X Save X Save X | Y Match X or Y {n} in variables named $ 1 ... Precise match N times {n,} Match N times or more {n, m} match the nm times [XYZ] character set (Character Set), match this collection

Any one of the characters (or meta) [^ XYZ] does not match any of the characters in this collection matches a back

The symbol / b matches the boundary of a word / b matches the non-boundary / cx of a word, X is a control, /

/ cm / match Ctrl-M / D matches a word number, // D / = / [0-9] / / D matches a non-character character, /

/ D / = / [^ 0-9] / / n matches a newline / r Match a return / s matching a blank character, package

Includes / n, / r, / f, / t, / v, etc. / s match a non-empty white character, equal to / [^ / n / f / r / t / v] / / t matching a tabby / V match A heavy straight table / w matches a character that can make a word (alphaumeric, this is my translation,

Included), including underscores, such as [/ W] matching 5.98 "5, equal to [A-ZA-Z0-9] / W Matching one

Composition of words, such as [/ W] match $ 5.98 "$, equal to [^ A-ZA-Z0-9].

Said so much, let's see examples of the practical application of some regular expressions:

E-MAIL address verification:

Function Test_Email (stremail) {

Var myreg = / ^ [_a-z0-9] @([A-Z0-9] ([_a-z0-9] /.) [A-Z0-9]{ 2, 3}

IF (MyReg.Test (stres) Return True;

Return False;

}

HTML code mask

Function Mask_htmlcode (STRINPUT) {

Var myreg = / <(/ w )> /;

Return Strinput.Replace (MyReg, "<$ 1>");

}

转载请注明原文地址:https://www.9cbs.com/read-38608.html

New Post(0)