简体   繁体   English

从字符串中删除字符的最后一个实例-Javascript-重新审视

[英]Remove Last Instance Of Character From String - Javascript - Revisited

According to the accepted answer from this question , the following is the syntax for removing the last instance of a certain character from a string (In this case I want to remove the last & ): 根据这个问题的公认答案,以下是从字符串中删除某个字符的最后一个实例的语法(在这种情况下,我想删除最后一个& ):

 function remove (string) { string = string.replace(/&([^&]*)$/, '$1'); return string; } console.log(remove("height=74&width=12&")); 

But I'm trying to fully understand why it works. 但是我试图完全理解它为什么起作用。

According to regex101.com, 根据regex101.com,

/&([^&]*)$/

& matches the character & literally (case sensitive) &匹配字符&按字面意义(区分大小写)

1st Capturing Group ([^&]*) Match a single character not present in the list below [^&]* 第一捕获组([^&]*)匹配[^&]*下面的列表中不存在的单个字符

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) *量词-在零和无限制的时间之间进行匹配,并尽可能多地匹配,并根据需要返回(贪婪)

& matches the character & literally (case sensitive) &匹配字符&按字面意义(区分大小写)

$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any) $声明位置在字符串末尾,或者在行终止符之前在字符串末尾(如果有)

So if we're matching the character & literally with the first &: 因此,如果我们将字符&从字面上与第一个&相匹配:

在此处输入图片说明

Then why are we also "matching a single character not present in the following list"? 那为什么我们还要“匹配以下列表中存在的单个字符”?

在此处输入图片说明

Seems counter productive. 似乎适得其反。

And then, " $ asserts position at the end of the string" - what does this mean? 然后,“ $声明字符串末尾的位置”-这是什么意思? That it starts searching for matches from the back of the string first? 它首先从字符串的后面开始搜索匹配项吗?

And finally, what is the $1 doing in the replaceValue? 最后, $1在replaceValue中的作用是什么? Why is it $1 instead of an empty string? 为什么用$1代替空字符串? ""

1- The solution for that problem I think is different to the solution you want: 1-我认为该问题的解决方案与您想要的解决方案不同:

That regex will replace the last "&" no matter where it is, in the middle or in the end of the string. 无论在字符串的中间还是结尾,该正则表达式都将替换最后一个“&”。

If you apply this regex to this two examples you will see that the first get "incorrectly" replaced: 如果将此正则表达式应用于这两个示例,您将看到第一个被“错误地”替换:

height=74&width=12&test=1
height=74&width=12&test=1&

They get replaced as : 它们被替换为:

height=74&width=12test=1
height=74&width=12&test=1

So to really replace the last "&" the only thing you need to do is : 因此,要真正替换最后一个“&”,您唯一需要做的就是:

string.replace(/&$/, '');

Now, if you want to replace the last ocurrence of "&" no matter where it is, I will explain that regex : 现在,如果您想替换“&”的最后一次出现,无论它在哪里,我都会解释一下regex:

$1 Represents a (capturing group), everything inside those ([^&]*) are captured inside that $1. $ 1代表一个(捕获组),那些([^&] *)中的所有内容都被捕获在该$ 1中。 This is a oversimplification. 这太简单了。

&([^&]*)$

& Will match a literal "&" then in the following capturing group this regex will look for any amount (0 to infinite) of characters (NOT EQUAL TO "&", explained latter) until the end of the string or line (Depending on the flag you use in the regex, /m for matching lines ). &将匹配文字“&”,然后在以下捕获组中,此正则表达式将查找任意数量(0到无穷大)的字符(不等于“&”,后面解释),直到字符串或行的末尾(取决于您在正则表达式中使用的标志/ m 用于匹配行 )。 Anything captured in this capturing group will go to $1 when you apply the replacement. 应用替换后,此捕获组中捕获的所有内容将变为$ 1。

So, If you apply this logic in your mind you will see that it will always match the last & and replace it with anything on its right that does not contain a single "&"" 因此,如果您将这种逻辑应用到您的脑海中,您将看到它总是与最后一个&匹配,并用右边的任何不包含单个“&”的东西替换它

&(<nothing-like-a-&>*)<until-we-reach-the-end> replaced by anything found inside (<nothing-like-a-&>*) == $1. In this case because of the use of * , it means 0 or more times, sometimes the capturing group $1 will be empty.


NOT EQUAL TO part: The regex uses a [^], in simple terms [] represents a group of independent characters, example: [ab] or [ba] represents the same, it will always look for "a" or "b". 与部分不相等:正则表达式使用[^],简单来说[]代表一组独立字符,例如:[ab]或[ba]代表相同字符,它将始终查找“ a”或“ b” 。 Inside this you can also look for ranges like 0 to 9 like this [0-9ba], it will always match anything from 0 to 9, a or b. 在其中,您还可以像[0-9ba]这样查找范围为0到9的值,它将始终匹配0到9,a或b的任何值。

The "^" here [^] represents a negation of the content, so, it will match anything not in this group, like [^0-9] will always match anything that is not a number. [^]此处的“ ^”表示内容的取反,因此,它将匹配该组中不存在的任何内容,例如[^ 0-9]将始终匹配不包含数字的任何内容。 In your regex [^&] it was used for looking for anything that is not a "&" 在您的正则表达式[^&]中,它用于查找不是“&”的任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM