【问题标题】:I am writing a regex to handle the following but i am stuck in between我正在编写一个正则表达式来处理以下内容,但我被困在两者之间
【发布时间】:2020-10-17 08:18:50
【问题描述】:

要求:

  • 字符串中不能有大写字母。
  • 字符串不能包含任何字符'^$.?*+()'
  • 如果字符串中存在“[”,则它后面必须跟零个或多个字符,而不是“[”和“]”,后者后面必须跟“]”。例如,[test] 有效,而 [test not valid, test] 和 [[test]] 无效。 [test][test] 有效。
  • '|' | 后面有的话可以用喜欢 |||无效但 |测试有效字|无效

function charPos(str, char) {
     return str
         .split("")
         .map(function (c, i) { if (c == char) return i; })
         .filter(function (v) { return v >= 0; });
   }

function testString(urlPattern)
{



let regex = /[ $^*()+\[\]\\|.\/?]/g;


if(regex.test(urlPattern)){
      let secondRegex =  true;
      let thirdRegex =  true;
      let regexData = /[ $^*()+\\\\.\/?]/g;
      let regexDataNew = /[ $^*()+\\\.\/?]/g;
      if(urlPattern.indexOf("[") < urlPattern.indexOf("]") && !regexData.test(urlPattern)){
        secondRegex =  false;
      }
      if(urlPattern.indexOf("[") == -1 && urlPattern.indexOf("]") == -1){
        secondRegex =  false;
      }
      if(urlPattern.indexOf("[[") != -1 || urlPattern.indexOf("]]") != -1){
        secondRegex =  true;
      }
      let pos = charPos(urlPattern,'|');
      let largest = pos.sort((a,b)=>a-b)[pos.length - 1];
      if(largest+1 < urlPattern.length && !regexDataNew.test(urlPattern)){
        thirdRegex =  false;
      }
      if(urlPattern.indexOf("|") == -1 ){
        thirdRegex =  false;
      }
      if(secondRegex || thirdRegex){
        return 'Not Valid';
      }
      else {
        return 'Valid';
      }
     
    }
    else {
        return 'Valid1';
    }

 }
    // test case

   testString('testttthhh@@|sss'); working
   testString('testttthhh@@|'); working
   testString('testttthhh@@|[]')  working but need to show invalid.

如果有人有解决方案或遇到相同类型的问题,请帮我解决。

谢谢

【问题讨论】:

  • 结帐Regex101。它包括字符串测试和针对多种风格的超级有用的快速参考指南,包括 ECMAScript (JavaScript)。
  • 我已经编辑了您的问题,试图使要求更加清晰。但是,您会看到第三点和第四点仍然不清楚。那是因为我不完全理解那里允许和不允许的内容。您需要编辑您的问题以使要点正确且明确。您还需要更改标题,我不想说,这很糟糕。 :-)

标签: javascript regex reactjs


【解决方案1】:

您可以使用以下正则表达式测试字符串。

/^(?!.*\|\S*\|)(?!.*[$^.?*+()A-Z])[^\[\]\n]*(?:\[[^\[\]\n]*\][^\[\]\n]*)*[^\[\]\n]*$/

Start your engine!

Javascript 的正则表达式引擎执行以下操作。

^                : match beginning of string
(?!              : begin negative lookahead
  .*\|\S*\|      : match 0+ chars followed by '|' follow by 0+
                   non-whitespace chars followed by |'
)                : end negative lookahead
(?!              : begin negative lookahead
  .*             : match 0+ chars
  [$^.?*+()A-Z]  : match a char in the char class
)                : end negative lookahead
[^\[\]\n]*       : match 0+ chars other than those char class
(?:              : begin a non-capture group
  \[             : match '['
  [^\[\]\n]*     : match 0+ chars other than those in char class
  \]             : match ']'
  [^\[\]\n]*     : match 0+ chars other than those in char class
)                : end non-capture group
*                : execute non-capture group 0+ times
[^\[\]\n]*       : match 0+ chars other than those char class
$                : match end of string

【讨论】:

  • Sir 2 case 未命中,字符串之间不允许有空格。如果 [] 表示 [empty] 则不允许。
【解决方案2】:

您可以使用匹配任何字符,除了您不想匹配的字符。

如果您到达竖线或左方括号,则断言该竖线后面跟着一个单词字符,例如 a-z、数字或下划线。

如果你遇到一个左方括号,你匹配它直到一个右方括号,并断言后面没有另一个。

如果不应匹配空字符串,则可以使用负前瞻 ^(?!$) 开始模式

^[^\s\[\]^$.|?*+()A-Z\\]*(?:(?:\[[^\s\[\]\\]+](?!\])|\|[^\s\[\]^$.|?*+()A-Z\\]+)[^\s\[\]^$.|?*+()A-Z\\]*)*$

解释

  • ^ 字符串开始
  • [^\s\[\]^$.|?*+()A-Z\\]* 匹配除所列字符外的任何字符 0+ 次
  • (?:非捕获组
    • (?:非捕获组
      • \[[^\s\[\]\\]+](?!\]) 匹配来自 [...] 后面没有 ]
      • |或者
      • \|[^\s\[\]^$.|?*+()A-Z\\]+ 匹配管道并匹配任何列出的单词字符的 1 次以上
    • )关闭非捕获组
    • [^\s\[\]^$.|?*+()A-Z\\]* 匹配除所列字符外的任何字符 0+ 次
  • )* 关闭非捕获组并重复 0+ 次,因为不必存在 |[]
  • $ 字符串结束

Regex demo

let pattern = /^[^\s\[\]^$.|?*+()A-Z\\]*(?:(?:\[[^\s\[\]\\]+](?!\])|\|[^\s\[\]^$.|?*+()A-Z\\]+)[^\s\[\]^$.|?*+()A-Z\\]*)*$/;
[
  "testttthhh@@|sss",
  "[]",
  "test test",
  "test\\test",
  "word|@pro",
  "word|%pro%",
  "testttthhh@@|",
  "testttthhh@@|[]",
  "[test]",
  "[test",
  "test]",
  "[[test]]",
  "[test][test]",
  "|||",
  "|test",
  "word|",
  "Atest"
].forEach(s => {
  console.log(pattern.test(s) + " --> " + s);
});

【讨论】:

  • 我们有办法确定所询问内容的详细信息,因此可能会进行更多修改。
  • 我注意到我的正则表达式比你的短(78 对 79 个字符)。
  • 如果这是CodeGolf,有人可能会找到一个包含 27 个字符的杀手级正则表达式。
  • 不久前我注意到的一件小事:虽然在[xyz\r?\n] 中包含\r? 通常是谨慎的,但我不知道是否需要将它添加到[^abc\n]
  • @Thefourthbird Sir 2 案例未命中,字符串之间不允许有空格。如果 [] 表示 [empty] 则不允许。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多