正则表达式:无法检查一行是否以单词结尾

时间:2023-01-14 17:12:05

As I wrote above, I want to GET (sorry I wrote check) a line if it doesn't end in a specific word ("done.") and it starts with this characters: "[-]".

正如我上面写的那样,如果它没有以特定的单词结尾(“完成”),我想得到(对不起,我写了支票)一行,并且它以这些字符开头:“[ - ]”。

Example:

例:

[-] Blabla bla bla. [✓]
[*] Blabla bla done. [X]
[.] Bla bla bla done. [X]

This is my try:

这是我的尝试:

   /\[-\](.*)[^done\.]$/gmi

Do you know how I can do this?

你知道我怎么做吗?

Thank you very much!

非常感谢你!

2 个解决方案

#1


2  

Just with this:

就这样:

/^\[-\](?!.*\bdone\.(?:[\n\r]|$))/mgi

(?!.*\bdone\.(?:[\n\r]|$)) is negative lookahead. Checking that the line is not ending with done.

(?!。* \ bdone \。(?:[\ n \ r] | $))是否定前瞻性。检查该行是否以done结尾。

^[-] is checking that the hyphen is at the beginning of the string.

^ [ - ]检查连字符是否在字符串的开头。

\b is a word boundary, so that it doesn't pick words like xyzdone.

\ b是一个单词边界,因此它不会选择像xyzdone这样的单词。

(?:[\n\r]|$) is checking either end of string, or at line breaks.

(?:[\ n \ r] | $)正在检查字符串的结尾或换行符。

#2


1  

Since you have tagged it as , you can do the below:

由于您已将其标记为javascript,因此您可以执行以下操作:

function check(input,word)
{
  return /^\[-].+/.test(input) && !new RegExp(word +"$").test(input);
}

Call it like this:

这样叫:

check('[-] e3r34 done.','done.'); // false
check('[-] e3r34 not.','done.'); // true

#1


2  

Just with this:

就这样:

/^\[-\](?!.*\bdone\.(?:[\n\r]|$))/mgi

(?!.*\bdone\.(?:[\n\r]|$)) is negative lookahead. Checking that the line is not ending with done.

(?!。* \ bdone \。(?:[\ n \ r] | $))是否定前瞻性。检查该行是否以done结尾。

^[-] is checking that the hyphen is at the beginning of the string.

^ [ - ]检查连字符是否在字符串的开头。

\b is a word boundary, so that it doesn't pick words like xyzdone.

\ b是一个单词边界,因此它不会选择像xyzdone这样的单词。

(?:[\n\r]|$) is checking either end of string, or at line breaks.

(?:[\ n \ r] | $)正在检查字符串的结尾或换行符。

#2


1  

Since you have tagged it as , you can do the below:

由于您已将其标记为javascript,因此您可以执行以下操作:

function check(input,word)
{
  return /^\[-].+/.test(input) && !new RegExp(word +"$").test(input);
}

Call it like this:

这样叫:

check('[-] e3r34 done.','done.'); // false
check('[-] e3r34 not.','done.'); // true