Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想用只包含负前瞻的正则表达式检查句子。所以如果写
^(?!My Words)$
和测试字符串是some characters。我希望正则表达式应该返回 true,但事实并非如此。是否应该有消耗匹配字符的字符来影响结果?
some characters
您提供的正则表达式意味着:
正则表达式中没有检查表明字符串实际上必须包含任何内容。更具体地说,它必须不包含任何内容。在这种情况下,前瞻不会做任何事情。
大概你想要更像这样的东西:
^(?!My Words).*$
.*只是零个或多个通配符,所以基本上任何东西。在这种情况下,前瞻会阻止字符串以“My Words”开头。
.*
如果你想防止“我的话”出现在任何地方,这样的事情应该可以工作:
^(?!.*My Words).*$