Simple use of regular position matching

Simple use of regular position matching

Write in front

This article is only a small analysis record of the regular application I have learned about a problem about string matching and replacement. Does not introduce regular knowledge in detail. If you want to know the content related to regular location matching, the real-name recommendation is https://zhuanlan.zhihu.com/p/27309508 produced by Yao.

Throw a problem

Enter a url, if there is no at the end, you /need to add it automatically.

function addSlash(url) {}

addSlash('y.qq.com/music');//=> "y.qq.com/music/"
addSlash('y.qq.com/music/');//=> "y.qq.com/music/"
addSlash('y.qq.com/music-');//=> "y.qq.com/music-/"
addSlash('y.qq.com/music-/');//=> "y.qq.com/music-/"

This problem is not difficult, but it feels that it is not too elegant to solve with if judgment, and regular matching is the right way.

Regular expressions are matching patterns, either matching characters or matching positions. —— Lao Yao

It is obvious here that you need to match the position, and then add it to the matched position /.

Based on the preliminary understanding of location matching, write:

function addSlash(url) {
   //Match the last position, this position is not in front of `/`
    return url.replace(/(?!\/)$/,'/');
}

addSlash('y.qq.com/music');//=> "y.qq.com/music/"
addSlash('y.qq.com/music/');//=> "y.qq.com/music//"
addSlash('y.qq.com/music-');//=> "y.qq.com/music-/"
addSlash('y.qq.com/music-/');//=> "y.qq.com/music-//"

Why is the last position still added /?

Remove the $, replace it with the global symbol, and see how it matches.

function addSlash(url) {
    return url.replace(/(?!\/)/g,'/');
}

addSlash('y.qq.com/');//=> "/y/./q/q/./c/o/m//"

For the understanding of position, we can understand it as a null character "". For example, the "hello" string is equivalent to the following form: "hello" == "" + "h" + "" + "e" + "" + "l" + "" + "l" + "o" + "";

Therefore: y.qq.com/is equivalent to ()y().()q()q().()c()o()m()/(), the brackets represent the position, and the middle of each character There is a "position" at the beginning and at the end.

So the position (?!\/)before the match is not /, that is, (/)y(/).(/)q(/)q(/).(/)c(/)o(/)m()/(/)

Clearly ()/this position is not coupled /, but the end (), because nothing behind this position, it really is not /, and therefore will be added.

Back to /(?!\/)$/the regular, it is the position of the end of the match and not the latter position /, the last character is not whether it /will be on a fix /.

solve

The above regularity does match the ending position, but the problem is that the "end position" is used as the judgment condition.

How to accurately match the ending position based on the last character? Here use the negative lookbehind in ES6?<!

That is, the match is a position, and the position is not in front of /

function addSlash(url) {
    return url.replace(/(?<!\/)$/,'/','/');
}

addSlash('y.qq.com/music');//=> "y.qq.com/music/"
addSlash('y.qq.com/music/');//=> "y.qq.com/music/"
addSlash('y.qq.com/music-');//=> "y.qq.com/music-/"
addSlash('y.qq.com/music-/');//=> "y.qq.com/music-/"

end

Although the article is short, it should be recorded. If there is a fallacy, please correct me and pat.

Later, we will write down more analysis records for a certain problem encountered.

Reference: https://cloud.tencent.com/developer/article/1579933 Simple use of regular location matching-Cloud + Community-Tencent Cloud