Home>

Regular expression,Also known as regular expression, regular expression, regular expression, regular expression, regular expression (English:regular expression, often abbreviated as regex, regexp or re in code

Regular expression,Also known as regular expression, regular expression, regular expression, regular expression, regular expression (English:regular expression, often abbreviated as regex, regexp or re in the code).

Regular expressions use a single string to describe and match a series of strings that match a certain syntax rule.In many text editors,Regular expressions are often used to retrieve and replace text that matches a pattern.

First, the character class

1. Character class:can match any character he contains

eg:/[abc]/matches any of the letters "a", "b", "c"

2. Negative character class:The negative character class is defined by the "^" character,It matches all characters not enclosed in square brackets.When defining a negative character class,Use a "^" symbol as the first character in the left parenthesis.

eg:/[^ abc]/matches all characters except "a", "b", and "c".

3. \ s:matches space, tab and other unicode whitespace;

.:Any character except newline and other unicode line terminators.

Second, repeat:

1. {n, m} matches the previous item at least n times, but not more than m times;

2. {n,} matches the previous item n times or more;

3. {n} matches the previous item n times;

4,?match the previous item 0 or 1 time;

5, * matches the previous item 0 or more times;

6. + matches the previous item one or more times.

ps:non-greedy repeat --- followed by a question mark after the characters to be matched. "??" "+?" "*?"

Selection, grouping, and quoting

1. "|" splits the alternative characters

eg:/ab | cd | ef/can match the string "ab", can also match the string "cd", and can also match the string "ef"

2. "()":The role of parentheses:a. Combine individual items into sub-expressions;b. Define sub-modes in the complete mode;c. Allow previous subexpressions to be referenced after the same regular expression.

3. "(? ;:" and ")" are grouped,But does not remember characters that match the shuffling

Fourth, specify the matching position:

1. Reasonable place for matching to occur,Anchor for regular expressions.

2. "^" matches the beginning of the string

3. "$" matches the end of the string

4. Any regular expression can be used as an anchor point.If you add an expression between the symbols "(?=" and ")",It's a lookahead assertion.

5. Negative lookahead assertions "(? ;!" and ")"

V. Modifiers:

1.i is not case sensitive

2.g global matching

3.Match in multiple lines

Six, regexp method

------- string method -------

1. search () returns the position of the first matching string;

2.repalce () performs search and replace operations

If the regular expression sets the modifier g, all strings in the source string that match the pattern will be replaced with the string specified by the second parameterWithout the g modifier, only the first matching substring is replaced.

3.match () returns an array of matching results

------- regexp method -------

4, exec ()

5, test ()

======================== Some common regular expressions ======================= ===

1. Remove whitespace characters (including space characters, tabs, and other unicode whitespace characters)

var pattern =/(^ \ s *) | (\ s * $)/g;

"abc def" .replace (pattern, "");//The execution result is:"abc def"

2. Strong password:The password is 8-12 digits, and all uppercase letters, lowercase letters, numbers, and special characters are included.

var strongregexp=/^(?=.{8,12})(?=.*[az])(?=.*[az])(?=.* [0-9]) (?=. * \ W). * $/G;

3. Weak password:The password is 7-12 digits and contains any two of uppercase letters, lowercase letters, and numbers.And does not contain special characters

var mediumregexp=/^(?=.{7,12})(((?=.*[az])(?=.*[az]))|((?=. * [Az]) (?=. * [1-9])) | ((?=. * [Az]) (?=. * [1-9 ]))). * $/g;

  • Previous SQL Server 2008 R2-find minimum nIndex, nIndex exists but nIndex + 1 does not exist Find the maximum value in the smallest conti
  • Next Eight ways to organize JavaScript to create objects