Learn the basics of regular expressions
This article talks about the basics of regular expressions of JavaScript. If you are not familiar with JavaScript regular expressions, you can just come and learn them. It talks about the basics of JavaScript. Regular expressions, for those who are not familiar with them, let’s take a look!
Regular expressionLearning (continuous updates)
Today I learned the RegExp object when I was learning javascript , take the opportunity to learn regular expressions. I have never been exposed to them before, so I take the opportunity to learn them. It is very comfortable.
Reference website: 30-minute introductory tutorial on regular expressionsClick to open the link
1. What is a regular expression
Regular expressions are used to express string matching rules.
2. Related concepts of regular expressions
2.1Metacharacters
Metacharacters are defined in regular expressions A special symbol added to regular expressions to replace certain rules.
\b | represents the beginning or end of a word |
. | represents except line breaks Any character other than |
* | represents any number of the character that appears before *, such as a*, which represents any number of a before it (repeat 0 times or multiple times) |
means + any number of the characters that appear before , such as a+, means there is any number of a in front (repeated one or more times) |
Repeat 0 or 1 times | Repeat greater than or equal to n times |
{n, m} | Repeat n to m times |
represents a number from 0 to 9 |
Matches letters or numbers or underscores or Chinese characters tab character, Newline characters, Chinese full-width spaces, etc. |
##^ | |
# & | ## The end of the matching string
Matches any characters that are not letters, numbers, underscores, or Chinese characters | |
Matches any character that is not a digit | |
Matches any character that is not the beginning or end of a word The characters at position | |
match any character that is not whitespace | |
Match characters other than x | |
Match characters other than aeiou |
For example, the regular expression ^\S+& is used to match a string of characters that does not contain whitespace charactersString
9. Back reference
The content of this part matches the previous grouping. When we use () to group characters, we can use this grouping by numbering The method will be quoted later. For grouping by (), grouping starts from 1 in the order of ( (). For example, the regular expression \b(\w+)\s+\1\b can be used to match repeated words. , such as go go, etc. Here, the group that appeared before is referenced through \1
## Other involved back reference syntax is:
Match exp and capture the current content into automatic groupings name>exp) | Match exp, and capture the current content and assign the group name name |
(?:exp) | Match exp, do not assign the captured content group name |
10. Zero-width assertionis used to find the part before or after a certain part of content but does not include the content. Regular expression (?=exp) means to assert that the part that appears later can match the expression exp. For example, \b\w+(?=ing\b) matches the front part of words ending in ing. For example, when searching for I'm dancing and singing, dance and singing will be matched (because there is \w+, it will not be matched as s). Regular expression (?<=exp) means to assert that the previous part can match the expression exp. For example, (?<=\bre)\w+\b will match the second half of the word starting with re. For example, when searching for reading, ading will be matched. If you want to add a comma to every three digits of a very long number, such as adding a comma to 123456789, you can use the regular expression ((?<=\d)\d {3})+\b, the search result is 234567890 (I don’t understand this part of the search rules...) The following example uses two assertions at the same time (?=<\ s)\d+(?=\s), used to match numbers between two blank characters, excluding blank characters. Generally speaking, the purpose of zero-width assertion is to determine the starting point or ending point of matching characters according to certain rules. 11. Negative zero-width assertionAs mentioned earlier, use antonyms to find characters that are not a certain character or are not in a certain character. characters. For example, if you want to find a word where the letter q appears but is not followed by u. You might write \bq[^u]\w*\b. But for such an expression, an error will occur when q appears at the end of a word, because [^u] will match the separator character of the word, which will then match the next word, which will match characters such as Iraq fighting. string. In order to solve the antonym occupancy problem, we can use a negative zero-width assertion, because it only matches one position and does not consume any characters. The above expression can be written as \bq(?!u)\w*\b. ## Similarly, we use (? ## A more complex example: (?<=<(\w+)>).*(?=<\/\1> ) When you see the previous (?<=) and the following (?=), you know that both the front and back are zero-width assertions, and <( \w+)> represents the html tag. If the previous one is 12. Comments Include comments through the syntax (?#comment), For example, 2[0-4]\d(?#200-249). 13. Greed and laziness Similar to the flag in js, there are case insensitivity, multi-line mode, global mode, etc. This part is to deal with the matching problem, for example, if you want to match the mathematical expression (5*3))) (5*3) cannot simply be written as \(.*\), as this will match the entire expression. Then the matching strategy that should be adopted is similar to the bracket matching problem we have learned. Use the stack to solve it. When encountering (pressing the stack, encountering) popping the stack, if the last stack is empty, this means that the brackets in the expression completely match. If If not empty, the regex engine will backtrack to make the brackets match. Related recommendations: How to use regular expressions in JS |
The above is the detailed content of Learn the basics of 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

Website security has attracted more and more attention, and using the HTTPS protocol to ensure the security of data transmission has become an important part of current website development. In PHP development, how to use regular expressions to verify whether the URL is HTTPS protocol? here we come to find out. Regular expression Regular expression is an expression used to describe rules. It is a powerful tool for processing text and is widely used in text matching, search and replacement. In PHP development, we can use regular expressions to match http in the URL

Sharing tips on using PHP regular expressions to implement the Chinese replacement function. In web development, we often encounter situations where Chinese content needs to be replaced. As a popular server-side scripting language, PHP provides powerful regular expression functions, which can easily realize Chinese replacement. This article will share some techniques for using regular expressions to implement Chinese substitution in PHP, and provide specific code examples. 1. Use the preg_replace function to implement Chinese replacement. The preg_replace function in PHP can be used

Python, as a high-level programming language, is easy to learn and use. Once you need to write a Python program, you will inevitably encounter syntax errors, and expression syntax errors are a common one. In this article, we will discuss how to resolve expression syntax errors in Python. Expression syntax errors are one of the most common errors in Python, and they are usually caused by incorrect usage of syntax or missing necessary components. In Python, expressions usually consist of numbers, strings, variables, and operators. most common
