简体   繁体   English

REGEX匹配//但不是://

[英]REGEX match // but not ://

I am new to regex and trying to learn it. 我是regex的新手并且正在努力学习它。 I am writing code in Coffeescript and want to match // for comments but not :// so it does not change the color of links. 我在Coffeescript中编写代码并希望匹配//以获取注释但不是://因此它不会更改链接的颜色。 This is for a grammar file for atom text editor. 这是用于原子文本编辑器的语法文件。 Any help would be appreciated. 任何帮助,将不胜感激。

The most easiest pattern I could come up with is by using a negative look behind like this: 我能想出的最简单的模式是使用这样的负面外观:

(?<!:)//

But look behinds are not supported by javascript (not sure about Atom itself, but Sublime supports it), so what I could think of is by using a regex like this: 但javascript不支持后面的观点(不确定Atom本身,但是Sublime支持它),所以我能想到的是使用这样的正则表达式:

.*:\/\/.*|(\/\/.*)

Working demo 工作演示

The idea is to use the discard technique, so the pattern will intentionally match what you don't want but will capture what you actually want. 我们的想法是使用丢弃技术,因此模式会有意地匹配您不想要的但会捕获您真正想要的内容。 Then you have to grab the content from the capturing group. 然后,您必须从捕获组中获取内容。

Match information: 比赛信息:

Group 1.    11-26   `// some comment`
Group 1.    41-59   `// another comment`

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

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