Skip to content

Forward slash is not escaped #30

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

Closed
septagram opened this issue Sep 21, 2020 · 5 comments · Fixed by #40
Closed

Forward slash is not escaped #30

septagram opened this issue Sep 21, 2020 · 5 comments · Fixed by #40

Comments

@septagram
Copy link

igornovikov1@~/Documents/Code/wtf-test:$ npm i escape-string-regexp
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN wtf-test@1.0.0 No description
npm WARN wtf-test@1.0.0 No repository field.

+ escape-string-regexp@4.0.0
added 1 package from 1 contributor and audited 1 package in 2.12s
found 0 vulnerabilities

igornovikov1@~/Documents/Code/wtf-test:$ node
> const escape = require('escape-string-regexp')
undefined
> escape ('^test/test$')
'\\^test/test\\$'
@papb
Copy link

papb commented Sep 21, 2020

Why should it? Running new RegExp('\\^test/test\\$') works fine.

@septagram
Copy link
Author

septagram commented Sep 24, 2020

I suppose this is reasonable, but I'd appreciate implementing it anyway (as it does no harm AFAIK) or highlighting it in the docs. My case is a little unusual, I'm passing the regexp into the DefinePlugin in order to use the regexp as a require.context argument -- otherwise non-matching files would be compiled anyway. So, I wrongly expected the slashes to be escaped and ended up with an incredibly arcane error a few weeks later.

@sindresorhus
Copy link
Owner

Package description:

Escape RegExp special characters

It would be both surprising and incorrect if we escaped other characters.

@sindresorhus
Copy link
Owner

I guess we could make it clearer in the readme that only the minimal amount of escaping is done.

@septagram
Copy link
Author

Thanks, that would work :)

slevithan added a commit to slevithan/escape-string-regexp that referenced this issue Jun 11, 2024
…edge cases

There have been multiple requests to escape additional characters, so this makes it clearer that minimal escaping is done (per sindresorhus#30 (comment)). It also gives a solution for more involved context-aware escaping that handles numerous edge cases, for situations when developers need to insert an escaped string at an arbitrary position in a regex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants