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

replace functionality broken in 4 and 4.1 #3240

Closed
bodyslam opened this issue Oct 7, 2015 · 16 comments
Closed

replace functionality broken in 4 and 4.1 #3240

bodyslam opened this issue Oct 7, 2015 · 16 comments
Labels
repl Issues and PRs related to the REPL subsystem.

Comments

@bodyslam
Copy link

bodyslam commented Oct 7, 2015

Hello

There seems to be an issue with Node's implementation of replace. The behavior appears to escape the backslashes; please see the output below:

> dev twong$  node -v
v4.0.0
nyl7twong:dev twong$ node
> a = "\\a\\b";
'\\a\\b'
> a.replace(/\\\\/g, '\*')
'\\a\\b'
> a.replace(/\\/g, '\*')
'*a*b'
> a.replace(/\\/g, '\\')
'\\a\\b'

Now using node v4.1.2
nyl7twong:dev twong$ node -v
v4.1.2
nyl7twong:dev twong$ node
> a = "\\a\\b";
'\\a\\b'
> a.replace(/\\\\/g, '\*')
'\\a\\b'
> a.replace(/\\/g, '\*')
'*a*b'
> a.replace(/\\/g, '\\')
'\\a\\b'

Chrome's output (via console)

a = "\\a\\b"
"\a\b"
a
"\a\b"
a = "\\\\a\\\\b"
"\\a\\b"
a.replace(/\\\\/g, '\\');
"\a\b"
@bnoordhuis
Copy link
Member

I'm not sure what you're getting at. Are you confused by the REPL printing backslashes in escaped form?

@bodyslam
Copy link
Author

bodyslam commented Oct 7, 2015

My apologies I pasted a missing example which is core to the issue:

> nyl7twong:dispatcher twong$ node -v
v4.1.2
nyl7twong:dispatcher twong$ node
> a = '\\a\\b'
'\\a\\b'
> a.replace(/\\\\/, '\\')
'\\a\\b'
> 

The core issue here is the replace function; given the string
a = '\\a\\b';
I'd like to replace the double backslash with a single backslash using
a.replace(/\\\\/g, '\\')

Which gives me "\\a\\b" (see the examples above) in Node 4 and 4.1
The Chrome example above gives the expected output of "\a\b" using the same method

@bnoordhuis
Copy link
Member

There are no double backslashes in a = '\\a\\b', they are parsed to single slashes. You can check that yourself with console.log(a), it prints \a\b.

@Trott
Copy link
Member

Trott commented Oct 7, 2015

While there is no issue with .replace() functionality here, there is a confusing difference between the Node.js REPL and the console in Chrome.

Node.js REPL:

> a = "\\a\\b";
'\\a\\b'
> 

Chrome console:

> a = "\\a\\b";
"\a\b"
>

Not saying anything needs to be done necessarily, but worth noting at least, I think.

@Trott Trott added the repl Issues and PRs related to the REPL subsystem. label Oct 7, 2015
@Fishrock123
Copy link
Contributor

Node.js REPL:

I suspect the repl is not formatting strings. Could that be the case?

@silverwind
Copy link
Contributor

Firefox too turns a '\\' into '\' for console output, so the browser consensus seems to be to print backslashes unescaped in string literals. The downside to this is that one needs to escape them again when copy-pasting from repl into code.

@bricss
Copy link

bricss commented Oct 9, 2015

Seems not an issue at all.

@bnoordhuis
Copy link
Member

Agreed, closing.

@noscripter
Copy link

find the repl ouput confusing too.

@targos
Copy link
Member

targos commented Nov 10, 2015

I personally think browser's output is more confusing:
image

@lili21
Copy link

lili21 commented Aug 8, 2018

If you don't think it's a issue, How can I save a value like a\/b ?

I need to create a json file contain the regexp string stuff.

{
  "regex": "a\/b"
}

@bricss
Copy link

bricss commented Aug 8, 2018

Here you go:

var str = String.raw`a\/b`;

@lili21
Copy link

lili21 commented Aug 8, 2018

Thanks, didn't know that function before.

But I need to write the content fo a file and use JSON.stringify function. and That would make it become a\\/b.

@bricss
Copy link

bricss commented Aug 8, 2018

That's right. Then you can do new RegExp('a\\/b') to use it as regular expression from parsed JSON.

@lili21
Copy link

lili21 commented Aug 9, 2018

Thank you @bricss . really helpful.

@sivam93
Copy link

sivam93 commented Jul 23, 2019

hi ,
Can you help me out with the same problem.
Console.log prints single slach(]) in REPL for the below code

var user="DOMAIN\USERNAME"

console output:DOMAIN\USERNAME.

I am Trying to the user to connect to mssqlserver data base using mssql package.

After assigning the user, and when tried to print the config getting the below output
Code:
var config = {
server: 'DEV',
user: 'DOMAIN\USERNAME',
password: 'XXXX',
database: 'TEMP'
}

NODE REPL OUTPUT:
{ server: 'DEV',
user: 'DOMAIN\USERNAME',
password: 'XXX',
database: 'TEMP' }

Seems to be the same issue but because of this not able to connect to the db, getting error: ConnectionError: Login failed for user

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

No branches or pull requests

10 participants