Javascript正则表达式:仅匹配文本的结尾

Javascript正则表达式:仅匹配文本的结尾,javascript,regex,Javascript,Regex,我想找出以“Tr”、“Br”和linebreak(“\n”)结尾的多行文本,前面没有“u”(无空格和下划线)。例 案文1: Command1 Br Command2 Tr "Command1 Br \nCommand2 Tr " //match "Tr " at the end 案文2: Command3 con Tr Command4 Br "Command3 con Tr \nCommand4 Br " //match "Br " at the end 案文3: Comman

我想找出以“Tr”、“Br”和linebreak(“\n”)结尾的多行文本,前面没有“u”(无空格和下划线)。例

案文1:

Command1 Br 
Command2 Tr 

"Command1 Br \nCommand2 Tr " //match "Tr " at the end
案文2:

Command3 con Tr 
Command4 Br 

"Command3 con Tr \nCommand4 Br " //match "Br " at the end
案文3:

Command2 Tr 
Command3 con Br 
Command4 C_

"Command2 Tr \nCommand3 con Br \nCommand4 C_\n" //match "\n" at the end after C_
案文4:

Command1 Tr 
Command1 Br 
Command2 Tr _

"Command1 Tr \nCommand1 Br \nCommand2 Tr _\n" //no match because of " _" preceded "\n"
案文5:

Command1 Tr 
Command1 Br 
Command2 mt

"Command1 Tr \nCommand1 Br \nCommand2 mt\n" //match \n at the end after "mt"
案文6:

Command2 ht

"\n\nCommand2 ht\n" //match \n at the end after "ht"

您可以使用以下正则表达式提取这些匹配项:

/(?:^| [^_]|[^ ]_|[^ ][^_])([BT]r|\n)[\t ]*$/

详细信息

  • (?:^ |[^!]|[^]|[^][^][^!]
    -与三个备选方案之一匹配的非捕获组:
    • ^
      -行的开头
    • |
      -或
    • [^\uU]
      -空格和任何字符,但
    • |
      -或
    • [^]\u
      -除了空格和
      \u
    • |
      -或
    • [^][^\]
      -除了空格之外的任何字符,然后是除了
      之外的任何字符(因此,没有
      空格
      +
  • ([BT]r |\n)
    -捕获组1:
    Br
    Tr
    或换行符
  • [\t]*
    -0+水平空白(可替换为
    [^\S\r\n]
    以更好地覆盖水平空白)
  • $
    -字符串的最末端
var ss=[“Command1 Br\nCommand2 Tr”、“Command3 con Tr\nCommand4 Br”、“Command2 Tr\nCommand3 con Br\nCommand4 C\n”,
“命令1 Tr\n命令1 Br\n命令2 Tr\n”、“命令1 Tr\n命令1 Br\n命令2 mt\n”、“命令2 ht\n”];
风险值rx=/(?:^ ^【^ ^】|【^】|【^】[^】([BT]r |\n)[\t]*$/;
用于(ss的var s){
log(“在“+s”中查找匹配项);
m=接收执行器;
如果(m){
log(“发现:+JSON.stringify(m[1],0,4));
}否则{
日志(“未找到匹配项”);
}

}
感谢您的建议,但它不适用于\n。在4种情况中,哪一种不适用于此正则表达式?好吧!也许上面的4个例子是正确的,但是当应用到其他文本时是错误的。您的模式无法找到没有“\u3”的换行符(“\n”),例如“Command3 con Br\n命令4 C\n”。我需要在“C”之后匹配\n,然后您应该提供所有详细信息以及更好的示例。我已经更新了示例。希望我能从你那里得到有用的建议。谢谢!@USSR20120188:如果解决方案解决了你的问题,请考虑接受答案。