Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Dear, hello, everyone. I am “Front-end Xiaoxin”. 😇 has been engaged in front-end development and Android development for a long time


The position in the re is like the index of our array, which identifies the position of the character. What keywords do we use to indicate position in the re?

01. Match beginning: Off character (^)

001. Does it match the beginning (match)

Regular: / ^ he/gm

Is: the helloworld

Example: worldhello

Note: As shown in the following figure, the example string starts with he and is successfully hit.

002. Leading insert character (replacement)

Regular: / ^ / gm

Character to be replaced: helloWorld

Replacement result:

02. Matching end: dollar sign ($)

001. Does the end match (match)

Re: /ld$/gm Positive example: helloWorld Negative example: worldHello Note: As shown in the following figure, the positive example string matches the end of LD and is successfully matched.

002. Closing insertion character (replacement)

Insert character with the beginning

Word \ non-word boundaries: \b, \b

\b word boundary refers to the position between the word character \w and the non-word character \w. It also includes the word character to the beginning of the broken character and the monad character to the end of the dollar character. \b vice versa.

03. Hit the front of p :(? =p)

Regular: /? =o)/gm

Note: As shown below, the position before each O in the string was successfully found.

04. Hit all positions other than p :(? ! p)

Regular: /? ! e)/gm

Note: Everything in the string except e was successfully found, as shown below.

The position diagram in the re is sorted out in the following figure, which can be supplemented by language compatible versions:

05. Case Study:

001. Display numbers in the thousandths

  1. Numeric characters represent:\d
  2. Thousandth quantifier:{3}
  3. Positional substitution character:(? =p)
  4. Make a hit from tail to head

First edition:

Ah? It’s weird. It only matches one location, right? What if we want to match multiple times? Use +, but we’re going to use () to group things;

The second version:

Now you can see that more than one position matches the first, so how to remove? Try this (? ! ^);

The third version:

Graphical representation of the final re:

Slightly expanded: When the contents of our string are slightly changed, our re above will become a little bit less handy, as shown below:

What about this problem? In the string, the beginning of each 1 and the end of each 8 belong to the word boundary, so WE can use \B to match the non-word boundary, and the end of each group is the word boundary, as shown in the following figure:


Welcome to follow my public account “Front-end Xiaoxin students”, the first time to push original technical articles.

Position matching we first learn this, quickly copy the previous code in the re to try using the following tool, XDM🤭. The above content is learned from Lao Yao’s regular expression mini-book v1.1 edition, because part of the content is not understood, please read the original more learning.

Recommend your own navigation site: https://it200.cn/

Re writing test site: https://regex101.com/

The regular expression icon is https://jex.im/regulex/