简体   繁体   English

正则表达式需要匹配字符串中的第一个路径而不是后续

[英]Regex need match first path in string not subsequent

I am looking for a regex that will match the first line, but not the second.我正在寻找一个匹配第一行但不匹配第二行的正则表达式。 This is what I have /^github.com\\/[^\\\\\\n]+$/这就是我所拥有的/^github.com\\/[^\\\\\\n]+$/

github.com/reggi
github.com/reggi/example

https://regexr.com/3ld7o https://regexr.com/3ld7o

 var patternA = /^github.com\\/[^\\\\\\n]+$/ var patternB = /^github\\.com\\/.+/ var patternC = /^github.com\\/[^\\/\\n]+$/ var examples = function (pattern) { return [ !!'github.com/reggi/genesis/soup'.match(pattern), !!'github.com/reggi/genesis'.match(pattern), !!'github.com/reggi'.match(pattern), ] } console.log(examples(patternA)) console.log(examples(patternB)) console.log(examples(patternC))

^github.com\/[^\/\n]+$

I think you just had the wrong type of slash escaped.我认为您只是转义了错误类型的斜线。

https://regexr.com/3ld8v https://regexr.com/3ld8v

The pattern /^github\\.com\\/.+/ is enough for your need (remember to uncheck m - multiline when you test with regexr.com).模式/^github\\.com\\/.+/足以满足您的需要(记住在使用 regexr.com 测试时取消选中m - multiline)。

https://regexr.com/3ld7u https://regexr.com/3ld7u

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

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