Home > Web Front-end > JS Tutorial > Detailed introduction to the usage of JS regular expressions

Detailed introduction to the usage of JS regular expressions

php中世界最好的语言
Release: 2018-01-08 10:24:46
Original
1255 people have browsed it

This time I will show you how to use H5 to create special effects of fireworks particles. How to use H5 to create special effects? What are the precautions for making fireworks particle special effects in H5? The following is a practical case, let’s take a look.

Regular expressionUsage details

Introduction

Simply put, regular expression is a powerful method that can be used for pattern matching and replacement. Tool of. Its function is as follows:
Test a certain pattern of the string. For example, a input character string can be tested to see if a phone number pattern or a credit card number pattern exists in the string. This is called data validity verification.
Replacement text. You can use a regular expression to identify specific text in a document, which can then be deleted entirely or replaced with other text.
Extract a substring from a string based on pattern matching. Can be used to find specific words in a text or input field.

Basic Grammar

After having a preliminary understanding of the functions and effects of regular expressions, let's take a closer look at the syntax format of regular expressions.

The form of regular expression is generally as follows:

/love/The part between the "/" delimiters is the pattern to be matched in the target object. Users only need to put the content of the pattern that they want to find matching objects between the "/" delimiters. In order to enable users to customize pattern content more flexibly, regular expressions provide special "metacharacters". The so-called metacharacters refer to those special characters with special meaning in regular expressions, which can be used to specify the appearance pattern of their leading characters (that is, the characters in front of the metacharacters) in the target object.
The more commonly used metacharacters include: "+", "*", and "?".
The "+" metacharacter stipulates that its leading character must appear one or more times continuously in the target object.
The "*" metacharacter specifies that its leading character must appear zero or consecutive times in the target object.
The "?" metacharacter specifies that its leading object must appear zero or once in the target object.

Now, let us take a look at the specific application of regular expression metacharacters.

/fo+/Because the above regular expression contains the "+" metacharacter, it means that it can appear consecutively with "fool", "fo", or "football" in the target object after the letter f. Matches strings with multiple letters o.
/eg*/Because the above regular expression contains the "*" metacharacter, it means that zero or more consecutive occurrences of "easy", "ego", or "egg" in the target object can occur after the letter e. Matches strings with the letter g.
/Wil?/Because the above regular expression contains the "?" metacharacter, it means that it can be compared with "Win" or "Wilson" in the target object, etc. Zero or one letter l appears continuously after the letter i String matches.

Sometimes I don’t know how many characters to match. To accommodate this uncertainty, regular expressions support the concept of qualifiers. These qualifiers specify how many times a given component of the regular expression must occur to satisfy a match.

{n} n is a non-negative integer. Match a certain number of n times. For example, 'o{2}' does not match the 'o' in "Bob", but it does match both o's in "food".
{n,} n is a non-negative integer. Match at least n times. For example, 'o{2,}' does not match the 'o' in "Bob" but does match all o's in "foooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.
{n,m} m and n are both non-negative integers, where n <= m. Match at least n times and at most m times. For example, "o{1,3}" will match the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o?'. Please note that there cannot be a space between the comma and the two numbers.
In addition to metacharacters, users can also specify exactly how often a pattern appears in a matching object. For example, /jim {2,6}/ The above regular expression stipulates that the character m can appear 2-6 times in a row in the matching object. Therefore, the above regular expression can match strings such as jimmy or jimmmmmy.

After having a preliminary understanding of how to use regular expressions, let's take a look at how to use several other important metacharacters.
\s: used to match a single space character, including tab keys and newlines;
\S: used to match all characters except a single space character;
\d: used to match starting from 0 to 9;
\w: used to match letters, numbers or underscore characters;
\W: used to match all characters that do not match \w;
.: used to match except newline characters All characters except .

(Note: We can regard \s and \S and \w and \W as inverse operations of each other)

Now, let’s take a look at how to use regular expressions through examples Use the above metacharacters.

/\s+/ The above regular expression can be used to match one or more space characters in the target object.
/\d000/ If we have a complex financial statement, we can easily find all the amounts totaling thousands of yuan through the above regular expression.
In addition to the metacharacters we introduced above, regular expressions also have another unique special character, namely the locator. Locators are used to specify where the matching pattern appears in the target object. The more commonly used locators include: "^", "$", "\b" and "\B".
The "^" locator specifies that the matching pattern must appear at the beginning of the target string
The "$" locator specifies that the matching pattern must appear at the end of the target object
The "\b" locator specifies that the matching pattern must appear at the beginning of the target string Appearing at one of the two boundaries at the beginning or end of the target string
The "\B" locator specifies that the matching object must be located within the two boundaries at the beginning and end of the target string,
That is, the matching object is both It cannot be used as the beginning or end of the target string.
Similarly, we can also regard "^" and "$" and "\b" and "\B" as two sets of locators that are inverse operations of each other. For example: /^hell/ Because the above regular expression contains the "^" locator, it can match the string starting with "hell", "hello" or "hellhound" in the target object. /ar$/ Because the above regular expression contains the "$" locator, it can match strings ending with "car", "bar" or "ar" in the target object. /\bbom/ Because the above regular expression pattern starts with the "\b" locator, it can match the string starting with "bomb", or "bom" in the target object. /man\b/ Because the above regular expression pattern ends with the "\b" locator, it can match strings ending with "human", "woman" or "man" in the target object.
In order to facilitate users to set matching patterns more flexibly, regular expressions allow users to specify a range in the matching pattern without being limited to specific characters. For example:
/[A-Z]/The above regular expression will match any uppercase letter in the range from A to Z.
/[a-z]/The above regular expression will match any lowercase letter in the range from a to z.
/[0-9]/ The above regular expression will match any number in the range from 0 to 9.
/([a-z][A-Z][0-9])+/ The above regular expression will match any string consisting of letters and numbers, such as "aB0".

One thing that users need to pay attention to here is that you can use "()" in regular expressions to combine strings together. The content contained in the "()" symbol must also appear in the target object. Therefore, the above regular expression will not match a string such as "abc" because the last character in "abc" is a letter and not a number.
If we want to implement an "OR" operation in regular expressions similar to programming logic and select any one of multiple different patterns for matching, we can use the pipe character "|". For example: /to|too|2/ The above regular expression will match "to", "too", or "2" in the target object.
There is also a more commonly used operator in regular expressions, which is the negation operator "[^]". Different from the locator "^" we introduced earlier, the negation character "[^]" specifies that the string specified in the pattern cannot exist in the target object. For example: /[^A-C]/ The above string will match any character except A, B, and C in the target object. Generally speaking, when "^" appears inside "[]", it is regarded as a negation operator; when "^" is outside "[]", or there is no "[]", it should be regarded as a negative operator. locator.
Finally, when users need to add metacharacters to the regular expression pattern and find their matching objects, they can use the escape character "\". For example: /Th\*/ The above regular expression will match "Th*" instead of "The" in the target object.

After constructing a regular expression, it can be evaluated like a mathematical expression, that is, it can be evaluated from left to right and in a priority order. The precedence is as follows:

1.\ escape characters
2.(), (?:), (?=), [] parentheses and square brackets
3.*, +, ?, {n}, {n,}, {n,m} qualifier
4.^, $, \anymetacharacter position and order
5.|"OR" operation

Usage examples

In JavaScript 1.2, there is a powerful RegExp() object that can be used to perform regular expression matching operations. The test() method can check whether the target object contains a matching pattern and return true or false accordingly.
We can use JavaScript to write the following script to verify the validity of the email address entered by the user.
Regular Expression Object
This object contains a regular expression pattern and flags indicating how to apply the pattern.
 Syntax 1 re = /pattern/[flags]
 Syntax 2 re = new RegExp("pattern",["flags"])
 Parameters
 re
 Required. The variable name to be assigned to the regular expression pattern.
 Pattern
 Required. The regular expression pattern to use. If using syntax 1, separate patterns with the "/" character. If using syntax 2, enclose the pattern in quotes.
 Flags
 Optional. If using syntax 2, enclose flag in quotes. Flags can be used in combination. The following are available:
## The following example creates an object (re) containing a regular expression pattern and related flags to demonstrate the use of regular expression objects. In this example, the resulting regular expression object is used in the match method:

function MatchDemo()
  {
  var r, re; // 声明变量。
  var s = "The rain in Spain falls mainly in the plain";
  re = new RegExp("ain","g"); // 创建正则表达式对象。
  r = s.match(re); // 在字符串 s 中查找匹配。
  return(r);
  }
Copy after login

I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

How jQuery makes the value in the click drop-down box accumulate to the text box

ES6 class What are the functions of static methods

detailed explanation of vue.js syntax and common instructions

The above is the detailed content of Detailed introduction to the usage of JS regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template