help icon indicating copy to clipboard operation
help copied to clipboard

Backward incompatibility issue with regular expression: forward slash in square brackets.

Open ryancat opened this issue 5 years ago • 3 comments

  • Version: 12.11.1, 12.18.4
  • Platform: linux, macOS, windows

What steps will reproduce the bug?

Go to https://jdoodle.com/ia/3tF and try executing with nodejs v10, and v12 to see different results.

var a = /[/\\]/
console.log(a)

In node v10, the output regular expression would be /[\/\\]/, while in node v12, it's /[/\\]/. I believe the forward slash in brackets are not necessary, and may got removed in nodejs v12. But that may create backward incompatibility issues for anyone tries to handle cross platform regular expression issues on paths. For example, in windows we need to change the path divider from / to \, and that requires manual search and replace the result for the forward slash. We would need to search for \/ on node v10, but only / in node v12.

How often does it reproduce? Is there a required condition?

Every time.

What is the expected behavior?

Some backward compatible API that give same result to v10

ryancat avatar Oct 27 '20 20:10 ryancat

It is the same regex either way. The only difference is the stringification of the regex object, which was changed a while back to get rid of unneeded escape characters.

devsnek avatar Oct 27 '20 20:10 devsnek

Yes, It's the same regex. The problem here is backward compatibility for this change. Here is one use case:

In node v10, when we are using regular expressions, they need to go through some mutator layer to make sure the separators are set according to the OS environment. That's because the original regexp may be /[/\\]foo[/\\]bar/ (which may assume to run on both linux and windows) and /baz\/qux/ (which may assume only in linux).

To transform all these cases to assume windows, we would need to go through all regular expression source, and call pattern.source.replace('/\//', path.sep). This is where node v12 will cause issue, as path.sep will be "\\", and we get /[\\\\]foo[\\\\]bar/ for node v10 (valid regexp), and /[\\\]foo[\\\]bar/ for node v12 (invalid regexp).

To fix this, we would have to either require all consumers to not have regexp like /[/\\]foo[/\\]bar/ (assume to be used in linux and windos), which is hard to do. Or we would need to detect node version and treat the string differently, which is possible, but not ideal.

ryancat avatar Oct 28 '20 00:10 ryancat

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar May 11 '24 01:05 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jun 10 '24 01:06 github-actions[bot]