Introduction to regular expressions
Regular expression, also known as regular expressions. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept of computer science. Regular expressions are usually used to retrieve and replace text that conforms to a certain pattern (rule).
Many programming languages support string manipulation with regular expressions. For example, a powerful regular expression engine is built in Perl. The concept of regular expression was originally popularized by tool software in Unix (such as sed and grep). Regular expressions are usually abbreviated as "regex". The singular numbers include regexp and regex, and the plural numbers include regexps, regexes, and regexen.
Introduction
In front-end development, you will inevitably encounter the need to match the label. Such a simple requirement is just two angle brackets to wrap a label name, and then a pause.
/<[\w]+>.*<\/[\w]+>/g
, and then it matched perfectly<div>xx</div>
For content, the requirements are completed...
Wait, from a glance, the current regularity can indeed match various tags, but it can also match similar
<div>xx</p>
content. This means we need to keep the contents in the two angle brackets the same.
Regular grouping backtracking reference grouping()
The so-called grouping means putting the content to match in brackets (). The content in brackets can be regarded as a whole subexpression
/<([\w]+)>.*<\/([\w]+)>/g
Backtracking quote\N
Regular expressions also provide a mechanism for referencing the previous match grouping. Sometimes, we may find a submatch that will appear again next.
// The \1 in this expression is the first grouping in the entire expressionvar reg = /<([\w]+)>.*<\/\1>/ ('<div>xx</div>') // true ('<div>xx</p>') // false
This is the article about backtracking and quoting issues for grouping regular expressions. For more related backtracking and quoting contents of regular expressions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!