繁体   English   中英

我正在编写一个正则表达式来处理以下内容,但我被困在两者之间

[英]I am writing a regex to handle the following but i am stuck in between

要求:

  • 字符串中不能有大写字母。
  • 字符串不能包含任何字符 '^$.?*+()'
  • 如果 '[' 出现在字符串中,则它必须后跟零个或多个字符,而不是 '[' 和 ']',后者必须后跟 ']'。 例如,[test] 有效,而 [test not valid, 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.

如果有人有一些解决方案或面临相同类型的问题,请帮助我解决。

谢谢

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

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

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

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

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

解释

  • ^字符串开头
  • [^\s\[\]^$.|?*+()AZ\\]*匹配除所列字符外的任何字符 0+ 次
  • (?:非捕获组
    • (?:非捕获组
      • \[[^\s\[\]\\]+](?!\])匹配来自[ ... ]后不跟]
      • | 或者
      • \|[^\s\[\]^$.|?*+()AZ\\]+匹配 pipe 并匹配任何列出的单词字符的 1+ 倍
    • )关闭非捕获组
    • [^\s\[\]^$.|?*+()AZ\\]*匹配除所列字符外的任何字符 0+ 次
  • )*关闭非捕获组并重复 0+ 次,因为不必有| []存在
  • $字符串结尾

正则表达式演示

 let pattern = /^[^\s\[\]^$.|?*+()AZ\\]*(?:(?:\[[^\s\[\]\\]+](?.\])|\|[^\s\[\]^$?|.*+()AZ\\]+)[^\s\[\]^$?|;*+()AZ\\]*)*$/, [ "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); });

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

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

启动你的引擎!

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

暂无
暂无

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

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