Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What’s the point of (?!\*)? #853

Closed
valtlai opened this issue Jan 13, 2016 · 7 comments
Closed

What’s the point of (?!\*)? #853

valtlai opened this issue Jan 13, 2016 · 7 comments

Comments

@valtlai
Copy link
Contributor

valtlai commented Jan 13, 2016

I’m trying to understand the Prism source code. What’s the point of (?!\*) in the following line:

var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;

I know it’s a negative lookahead. In this regex it means that an asterisk doesn’t follow the hyphen.

But where is it needed?

@kylekatarnls
Copy link

HI, foo(?!bar) mean "take foo if it's not followed with bar". * is the * symbol. So it takes - if it's not followed with * then a word. So lang-php, language-html, but not lang-*php.

@valtlai
Copy link
Contributor Author

valtlai commented Jan 13, 2016

But how that regex differs from one without (?!\*)?

/\blang(?:uage)?-(?!\*)(\w+)\b/i.test('lang-*php');  // returns `false`
/\blang(?:uage)?-(\w+)\b/i.test('lang-*php');  // returns `false` too!

@kylekatarnls
Copy link

Ho, indeed, \w exclude *, you're right it's redundant. What's the line and file? With them we could know who wrote this and ask him/her if there is a reason.

@valtlai
Copy link
Contributor Author

valtlai commented Jan 13, 2016

It’s here: /components/prism-core.js:18

Added in cfaf681

@kylekatarnls
Copy link

It seems @LeaVerou added this piece of the RegExp specifically: cfaf681
But there are no differences when I test with examples.html classes.

@LeaVerou
Copy link
Member

Yeah, I have no idea why I added that, since as you pointed out, \w doesn't include * anyway. It can be safely removed.

@valtlai
Copy link
Contributor Author

valtlai commented Jan 16, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants