简体   繁体   English

负正则表达式在Java中落后

[英]Negative lookbehind regex in Javascript

I want to match all strings that start with an @ unless they have other characters in front of the @ as well. 我想匹配所有以@开头的字符串,除非它们在@前面也有其他字符。 Simple example: in @one @two and bla@three I want to match @one and @two but not @three . 简单的例子:在@one @two and bla@three我想匹配@one@two匹配@three It's for highlighting usernames in a chatroom. 用于突出显示聊天室中的用户名。

These strings can be anywhere in a sentence (right at the start or in the middle). 这些字符串可以在句子中的任何位置(在开头或中间)。

I actually thought that (?![a-zA-Z])@[a-zA-Z]+ should work but it still matches @three too. 我实际上认为(?![a-zA-Z])@[a-zA-Z]+应该可以,但是它仍然也匹配@three

You don't need a regex look around, you can use a simple regex like this: 您不需要查看正则表达式,可以使用如下所示的简单正则表达式:

\B@\w+

Working demo 工作演示

在此处输入图片说明

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

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