Detailed introduction to the usage of JS regular expressions
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); }
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 boxES6 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Users may have seen the term wapi when using the Internet, but for some people they definitely don’t know what wapi is. The following is a detailed introduction to help those who don’t know to understand. What is wapi: Answer: wapi is the infrastructure for wireless LAN authentication and confidentiality. This is like functions such as infrared and Bluetooth, which are generally covered near places such as office buildings. Basically they are owned by a small department, so the scope of this function is only a few kilometers. Related introduction to wapi: 1. Wapi is a transmission protocol in wireless LAN. 2. This technology can avoid the problems of narrow-band communication and enable better communication. 3. Only one code is needed to transmit the signal

Pubg, also known as PlayerUnknown's Battlegrounds, is a very classic shooting battle royale game that has attracted a lot of players since its popularity in 2016. After the recent launch of win11 system, many players want to play it on win11. Let's follow the editor to see if win11 can play pubg. Can win11 play pubg? Answer: Win11 can play pubg. 1. At the beginning of win11, because win11 needed to enable tpm, many players were banned from pubg. 2. However, based on player feedback, Blue Hole has solved this problem, and now you can play pubg normally in win11. 3. If you meet a pub

i5 is a series of processors owned by Intel. It has various versions of the 11th generation i5, and each generation has different performance. Therefore, whether the i5 processor can install win11 depends on which generation of the processor it is. Let’s follow the editor to learn about it separately. Can i5 processor be installed with win11: Answer: i5 processor can be installed with win11. 1. The eighth-generation and subsequent i51, eighth-generation and subsequent i5 processors can meet Microsoft’s minimum configuration requirements. 2. Therefore, we only need to enter the Microsoft website and download a "Win11 Installation Assistant" 3. After the download is completed, run the installation assistant and follow the prompts to install Win11. 2. i51 before the eighth generation and after the eighth generation

After updating to the latest win11, many users find that the sound of their system has changed slightly, but they don’t know how to adjust it. So today, this site brings you an introduction to the latest win11 sound adjustment method for your computer. It is not difficult to operate. And the choices are diverse, come and download and try them out. How to adjust the sound of the latest computer system Windows 11 1. First, right-click the sound icon in the lower right corner of the desktop and select "Playback Settings". 2. Then enter settings and click "Speaker" in the playback bar. 3. Then click "Properties" on the lower right. 4. Click the "Enhance" option bar in the properties. 5. At this time, if the √ in front of "Disable all sound effects" is checked, cancel it. 6. After that, you can select the sound effects below to set and click

PyCharm is a powerful Python integrated development environment with rich functions and tools that can greatly improve development efficiency. Among them, the replacement function is one of the functions frequently used in the development process, which can help developers quickly modify the code and improve the code quality. This article will introduce PyCharm's replacement function in detail, combined with specific code examples, to help novices better master and use this function. Introduction to the replacement function PyCharm's replacement function can help developers quickly replace specified text in the code

Many users have printer drivers installed on their computers but don't know how to find them. Therefore, today I bring you a detailed introduction to the location of the printer driver in the computer. For those who don’t know yet, let’s take a look at where to find the printer driver. When rewriting content without changing the original meaning, you need to The language is rewritten to Chinese, and the original sentence does not need to appear. First, it is recommended to use third-party software to search. 2. Find "Toolbox" in the upper right corner. 3. Find and click "Device Manager" below. Rewritten sentence: 3. Find and click "Device Manager" at the bottom 4. Then open "Print Queue" and find your printer device. This time it is your printer name and model. 5. Right-click the printer device and you can update or uninstall it.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold
