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 (?\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 ((? The following example uses two assertions at the same time (?= 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: (?).*(?= ) When you see the previous (? 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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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