A Brief Analysis of the Usage of the Shortest Matching Patterns in Regular Expressions

Time:2019-5-12

Preface

Recently, I wanted to use regular expressions to grab something out of the web page. The content is not complicated, but there are many problems. Next, let’s take a look at the detailed introduction.

When we use regular expressions to match the beginning and end of a tag, for example, matching<h1>hello world</h1> Start and Close Labels of H1 in

Maybe a lot of people will write like this.


/<.*h1>/g

But is that really okay?

Because the * matcher matches zero to more than one of the previous characters, and it is greedily matched

So what you get is the following.

Obviously this is not what we want, so how to replace greedy matching with minimum matching?


/<.*?h1>/g

The above description is OK, as follows:

In fact, the principle should be very simple, because? It is also a greedy match, and can only match 0 to 1.

So it will end up matching the first time, thus preventing * from matching multiple greed.

PS: Here are two very convenient regular expression tools for your reference.

JavaScript Regular Expressions Online Testing Tool:http://tools.jb51.net/regex/javascript

Online Generation Tool for Regular Expressions:http://tools.jb51.net/regex/create_reg

Readers who are interested in JavaScript-related content can see the topics on this site: JavaScript Regular Expressions Skills, JavaScript Replacement Operations Skills, JavaScript Search Algorithms Skills, JavaScript Data Structure and Algorithms Skills, JavaScript Traversal Algorithms and Skills, and Json Operational Skills in JavaScript. “Summary of JavaScript Errors and Debugging Skills” and “Summary of JavaScript Mathematical Usage”

summary

Above is the whole content of this article. I hope the content of this article can bring some help to everyone’s study or work. If you have any questions, you can leave a message to exchange. Thank you for your support to developpaer.