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

Escaped backslash before quote #15

Open
derek-miller opened this issue Oct 24, 2019 · 2 comments
Open

Escaped backslash before quote #15

derek-miller opened this issue Oct 24, 2019 · 2 comments

Comments

@derek-miller
Copy link

It appears the logic for when to respect a quote vs an escaped quote does not consider if the preceding escape char is itself escaped. For example:

$ node
Welcome to Node.js v12.3.1.
Type ".help" for more information.
> const split = require('split-string')
undefined
> console.log(split('\\\\"hello world\\\\"', { separator: ' ', quotes: ['"'] }))
[ '\\\\"hello', 'world\\\\"' ]
undefined
>

I would expect this to be [ '\\\\"hello world\\\\"' ].

Thoughts?

@doowb
Copy link
Collaborator

doowb commented Oct 24, 2019

I agree with this and did a quick test to see if adding the following here would help:

  const closeIndex = (value, startIdx) => {
    let idx = string.indexOf(value, startIdx);
    if (idx > -1 && (string[idx - 1] === '\\' && string[idx - 2] !== '\\')) {
      idx = closeIndex(value, idx + 1);
    }
    return idx;
  };

For this case it passes, but I haven't tried this change with the other tests. I'll try to get to this over the weekend unless someone else does a PR with the new test cases before that.

Thanks for the issue!

@derek-miller
Copy link
Author

@doowb Unfortunately that fix is too simplistic, if the string were \\\\\\"hello world\\\\" then the quote would be properly escaped but would still satisfy string[idx - 1] === '\\' && string[idx - 2] !== '\\'.

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

2 participants