(?<!")https:\/\/t.me\/(c)?\/?([\ a-zA-Z0-9] )\/?([0-9]*)?
I want to find all telegram links without quotation marks (") but I don't want the leading negative lookbehind to be a group, how can I do this? I tried the following but it didn't work.
This code works but i want the initial negative lookbehind not to create group.
My steps:
(?:(?<!"))
not worked,(?<!(?:"))
not worked either
Examples:
https://t.me/ AjFb2c8u85UfYrY0
-> True (1 group -> AjFb2c8u85UfYrY0) (not two groups)"https://t.me/ AjFb2c8u85UfYrY0
-> False
CodePudding user response:
This code:
const regex = /(?<!")https:\/\/t.me\/(c)?\/?([\ a-zA-Z0-9] )\/?([0-9]*)?/
const text = 'https://t.me/ AjFb2c8u85UfYrY0'
const [fullMatch, ...groups] = text.match(regex);
console.log(groups);
Returns [undefined, " AjFb2c8u85UfYrY0", undefined]
So you might think the first undefined
is because of your negative lookbehind, but it's not! The first undefined
is the result of the (c)?
capturing group.
So if you capture groups on a url with the /c/
pattern, here is what you'll get:
const regex = /(?<!")https:\/\/t.me\/(c)?\/?([\ a-zA-Z0-9] )\/?([0-9]*)?/
const text = 'https://t.me/c/ AjFb2c8u85UfYrY0'
const [fullMatch, ...groups] = text.match(regex);
console.log(groups);
So there is nothing you need to do for the initial negative lookbehind not to create group. It is already working.