This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Regular expression, this thing I am sure will make a lot of people headache a thing, today to liver a liver headache regular expression.

Introduction to Regular Expressions

Regular Expression: A Regular Expression is a logical formula for manipulating strings. It uses predefined characters and their sum to form a “rule string”. The rule string is used to filter strings.

Functions of regular expressions

  1. Whether the given string conforms to the filtering logic of the regular expression (matching).
  2. We can use regular expressions to get the specific part (extract) we want from the string.
  3. Powerful string substitution capability (substitution).

Features of regular expressions

  1. Flexible, logical and functional.
  2. Complex control of strings can be achieved quickly and in a very simple manner.

Regular expression syntax

Online test re

Composition of a regular expression

  • The common characters ABC, Chinese 123, etc
  • Special characters (metacaracters, qualifiers, and brackets) : Characters that have special meanings in a regular expression.

metacharacters

Metacharacters are characters that have special meanings in regular expressions.

metacharacters instructions
\d Match the Numbers
\D Matching non-number
\w Matches letters or numbers or underscores _
\W Matches letters, digits, and underscores
\s Match whitespace (Spaces)
\S Matches non-whitespace characters
. Matches any single character other than the newline (carriage return key)

qualifiers

A qualifier is a limit on the number of characters that can occur

qualifiers instructions
n* Matches any string containing zero or more n’s. n{0,}
n+ Matches any string containing at least one n. n{1,}
n? Matches any string containing zero or one n. {0, 1}
n{x} Matches a string containing x sequences of n.
n{x,} Matches a string that contains at least x sequences of n.
n{x,y} Matches a string that contains at least X sequences and at most y sequences of n.
n$ Matches any string ending in n.
^n Matches any string that starts with n.
Note:
  1. When a regular expression is used, the whole string is checked to ensure that the string matches only if it matches completely. Otherwise it doesn’t match.
  2. For the number limit in curly braces, when matching a string, the previous order is matched first, and the maximum number is matched first. If the maximum number does not meet the requirements, the smaller number is matched.
  3. If the number limit in curly braces is at least 0, no matching item in the string meets the requirement, and no matching character in the string meets the requirement.

brackets

A bracket represents a string, and the purpose of the bracket is to control the range of a character.

brackets instructions
[abc] Finds any character between parentheses.
[^abc] Looks for any character that is not in between square brackets, where ^ has an inverse in brackets.
[0-9] Look for any number from 0 to 9.
[a-z] Looks for any character from small a to lowercase Z.
[A-Z] Finds any character from capital A to capital Z.
[A-z] Find a letter (case and underscore).
[[\u4e00-\u9fa5]] Find a Chinese character.

In the match, the search is performed from the front to the back according to the [rule]. If there is a global match, the match starts from the next one when there is a match.

Or pattern

Special symbols: regular 1 | 2, regular or. It matches or one of the two sides.

For example: Google, Baidu, Bing; // Matches one of the three strings

Regular: Google | baidu | bing

Grouping pattern

Special symbols: (regular);

Group refers to a small group, grouping is a large group can be divided into several small groups.

For example: control the number of consecutive occurrences of your name, from a minimum of 1 to a maximum of 3

Regular: ^ (Bruce) ${1, 3}

The modifier

  • G, global, means global match. Such as:var reg = /hello/g;
  • I, ignore, means you can ignore the case of a letter when you match it. Such as:var reg = /hello/i;
  • Gi, global matching and ignoring case are used together. Such as:var reg = /hello/gi;

The regular escape character

Represents a special symbol in a re. To remove the special meaning of.

Js uses regular expressions

Regular expression object

  • Create a regular expression object
Var var = new RegExp(" rule "," modifier ") */
var reg = new RegExp('\\d'.'g')
/** Method 2: Syntax: var variable = / regular expression/(recommended) */
var reg = /\d/g;
Copy the code
  • Detect matches: regular object test(string); Checks whether a string matches a rule. Returns true and false

String object methods related to regular use

  • Match (regular object); Gets the substring that matches the re and returns an array.
  • Replace (regular object, the contents after the replacement); Replaces the substring of the matched re. Returns the replaced string.

Praise support, hand stay fragrance, and have glory yan, thank you for leaving your footprints.

If you’re interested, leave a comment below with some regular expressions you use frequently