简体   繁体   English

检查字符串是否是javascript中未知子字符串的重复

[英]Check if string is repetition of an unknown substring in javascript

I try to solve the same problem in javascript with regexp mentioned here: Check if string is repetition of an unknown substring 我尝试使用此处提到的regexp解决javascript中的相同问题: 检查字符串是否是未知子字符串的重复

I translated the regex in the first answer to Javascript: ^(.+){2,}$ But it does not work as I expect: 我在Java脚本的第一个答案中翻译了正则表达式: ^(.+){2,}$但它不能按我预期的那样工作:

'SingleSingleSingle'.replace(/^(.+){2,}$/m, '$1')  // returns 'e' instead of exptected 'Single'

What am I overlooking? 我在俯视什么?

I currently have no explanation for why it returns e , but . 我目前还没有解释为什么它返回e ,但是. matches any character and .{2,} basically just means "match any two or more characters". 匹配任何字符,而.{2,}基本上仅表示“匹配任何两个或更多字符”。

What you want is to match whatever you captured in the capture group, by using backreferences: 您想要的是通过使用反向引用来匹配您在捕获组中捕获的任何内容:

/^(.+)\1+$/m

I just noticed that this is also what the answer you linked to suggests to use: /(.+)\\1+/ . 我只是注意到,这也是您链接的答案建议使用的: /(.+)\\1+/ .+) /(.+)\\1+/ The expression is exactly the same, there is nothing you have to change for JavaScript. 表达式完全相同,您无需为JavaScript更改任何内容。

I think the reason why you get 'e' is that {2,} implies two or more repetitions of a match to the regex that preceeds it, in this case (.+) . 我认为您得到'e'的原因是{2,}意味着与之匹配的正则表达式(.+)在本例中为(.+)要进行两次或多次重复。 {2,} does not guarantee that the repetitions match each other , only that they all qualify as a match for (.+) . {2,}不保证重复内容彼此匹配,仅保证它们都符合(.+)的匹配条件。

From what I can see (using Expresso ) it looks like the first match to (.+) is 'SingleSingleSingl' (due to greedy matching) and the second match is 'e'. 从我所看到的(使用Expresso )看来, (.+)的第一个匹配是“ SingleSingleSingl”(由于贪婪匹配),而第二个匹配是“ e”。 Since capturing groups only remember their last match, that is why replace() is giving you back 'e'. 由于捕获组仅记住它们的最后一场比赛,因此,replace()会给您返回“ e”的原因。 If you use (.+?) (for non-greedy or reluctant matching) each individual character will match, but you will still only get the last one, 'e'. 如果您使用(.+?) (用于非贪心或勉强匹配),则每个字符都将匹配,但您仍然只会得到最后一个字符“ e”。

Using a back reference, as Felix mentioned, is the only way that I know of to guarantee that the repetitions match each other. 正如Felix提到的那样,使用反向引用是我所知道的确保重复彼此匹配的唯一方法。

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

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