简体   繁体   English

正则表达式匹配重复相同的单词或其中的一部分

[英]Regexp for match repetition of same word or part of it

I give up: I simply can not find a solution to the problem.我放弃了:我根本找不到解决问题的办法。 I've already checked for hours on stack overflow site but I can not find a solution.我已经在堆栈溢出站点上检查了几个小时,但找不到解决方案。 I have these strings:我有这些字符串:

table-2 table
table-4 seat
table-4 seat table-3

I want to use a regular expression to see if it already contains 'table-' or 'table' or 'tabl' or 'tab' ...我想使用正则表达式来查看它是否已经包含 'table-' 或 'table' 或 'tabl' 或 'tab' ...

(?).test(string) === true only in Example N.2. (?).test(string) === true仅在示例 N.2 中。 How can I do that?我怎样才能做到这一点?

This was a attempt:这是一次尝试:

/\b([a-z(-0-9)?]+)\b(?=.*(\b)+)/g

This code snipped passes your test criteria:此代码剪断通过了您的测试标准:

[
  'table-2 table',
  'table-4 seat',
  'table-4 seat table-3',
].forEach(word => {
  console.log(word, !/(tabl?e?\-?).*?\1/.test(word));
})

UPDATE For the case if there is no exact word available (see comment below) it is possible to just check for some existence of some repeated substring that by itself forms a kind of word.更新对于如果没有可用的确切单词(请参阅下面的评论)的情况,可以只检查某些重复的子字符串是否存在,这些子字符串本身就形成了一种单词。 In this case test may look like: !/\\b(\\S+)\\b.*?\\1/.test(word) , but of course exact solution may be different depending on actual task.在这种情况下, test 可能看起来像: !/\\b(\\S+)\\b.*?\\1/.test(word) ,但当然确切的解决方案可能会因实际任务而异。

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

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