Understanding of regular assertion in JavaScript


When I was a junior studying compiler principles, the teacher said something called assertion, but I didn’t understand this course at all, so I couldn’t find anything valuable. Well, forgive me for being lazy and ignorant. The theory is not suitable for me. I’m only suitable for moving bricks. After a long time, some black technology about regularization found that there are more assertions in regular. Well, we have to understand what this is.

So what are the assertions in regular?

Quote a note from the encyclopedia

They are used to find things before or after certain contents (but not including them), that is, they are used to specify a position like B, ^, $, which should satisfy certain conditions (i.e. assertion), so they are also called zero width assertions. (from Baidu Encyclopedia)

Assertions are also called assertions in some textbooksLook around

Assertion writing

  • Forward looking assertion

    • (?=exp)Look around positively in order to show that the right side of the position can match exp

    • (?!exp)Sequential negative look indicates that the right side of the position cannot match exp

  • Backward assertion

    • (?<=exp)Looking around in reverse order means that the left side of the position can match exp

    • (?<!exp)Negative look in reverse order means that the left side of the position cannot match exp

Forward looking assertion (testing forward from current position) and backward assertion (testing backward from current position).

Specific examples to see how to use it.


  • Parentheses are required, written as: (?! =…)

  • Some languages are not fully supported, for example: Javascript does not support regularBackward assertionExpression, used will report error.

  • amongexpIs a regular expression, which can be a sub pattern, such as:(?=((exp))

  • Look around is also known as assertion; assertion does not occupy the string ((? = exp), so it cannot be referenced, exp is used for string)

usage method

  • (?=exp)Look around positively in order to show that the right side of the position can match exp

Let’s take a JavaScript example, match.gifFile name of

var s="img.jpg,abc.gif,123.jpeg";


/\w*(?=\.gif)/Medium\w*Indicates that there can be zero or more characters, matching isabcSo (? =. GIF) what does this match to? In fact, what he matches is only a position, which is the original intention of the assertion. What he matches is betweenabcAnd.gifThe location of.
Well, I don’t believe it
Remove regular expression\w*, replace the matched content with#

var s="img.jpg,abc.gif,123.jpeg";


Back to the definition:(?=exp)Look around in order, indicating that the right side of the position can match exp; then/\w*(?=\.gif)/It means to use.gifIf the position on the right is successful, it will matchabc.gifIt is successful here. As we said before, assertion matching does not take up character width (that is, no result will not contain assertion part), so the matching string is\w*That is’ ABC ‘.

  • (?!exp)Sequential negative look indicates that the right side of the position cannot match exp

Example: matching non.gifFile name of

var s="img.jpg,abc.gif,123.jpeg";

result:["img.jpg", "123.jpeg"]

/(\w*)(?:\.)(?!gif)\w*/gThis regular representation(\w*)(?:\.)The right side of is not a GIF match.

aboutBackward assertionIf you have time, please fill in.

Examples of use

Let’s take a question and answer example from the network: count a string10000000000Words with,Divided into10,000,000,000

This is an example0Let’s start with the simple one. OK, let’s change a string of strings, such as:12345678convert to12,345,678

How to implement regularization?
Look at the code:

var s="12345678";

Yes, that’s it.
Well, here’s how it works

/(?=(\d{3})+(?!\d))/gIn which(\d{3})+Indicates that there is at least one set of the preceding three digit strings, followed by(?!\d)It means the right side of a number, not a number,
So that’s the end, then/(?=(\d{3})+(?!\d))/The matching position is12And345678Between the position, plusgTable global matching,
So continue to match the345And678Between the positions in these plus,That’s it12,345,678Yes.

If you want to rewrite the code as follows:

var s="12345678";
s.replace(/(?=((\d{3})+)(?!\d))/g,function(){console.log(arguments);return ","});

Cycle 1:["", "345678", "678", 2, "12345678"]
Cycle 2:["", "678", "678", 5, "12345678"]
Return value:"12,345,678"

Well, the principle will not elaborate, copy the code to the browser console to see the effect.

So this one10000000000Into10,000,000,000It’s very simple.

But if the number of digits in the string is a multiple of 3, there is also one in front of the string,Okay, we’ll get rid of themresult.replace(",","")


Some of the above examples may not be enough to illustrate the problem, if you are a master of regular, hope not to spray, of course, more advice that would be better.

Recommended Today

Summary of recent use of gin

Recently, a new project is developed by using gin. Some problems are encountered in the process. To sum up, as a note, I hope it can help you. Cross domain problems Middleware: func Cors() gin.HandlerFunc { return func(c *gin.Context) { //Here you can use * or the domain name you specify c.Header(“Access-Control-Allow-Origin”, “*”) //Allow header […]