Regex将替换在节点中与在控制台中工作的不一样

时间:2021-01-05 16:52:16

I need to escape all commas in a sentence. My replace isn't working correctly in node, however in chrome console it's working fine.

我需要把句子中的所有逗号都转义。我的替换在node上不能正常工作,但是在chrome控制台它可以正常工作。

Does anyone have a solution to this? It seems to be the same for all characters

有人能解决这个问题吗?似乎所有的角色都是一样的

let sentence = 'Hello, my name is Jim'

让句子= '你好,我的名字是吉姆'

sentence.replace(/,/g, '\\,');

句子。替换(/,/ g,' \ \ ');

OUTPUT ON NODE TERM: 'Hello\\, my name is Jim'

节点词输出:'Hello\, my name is Jim'

The final string should be 'Hello\, my name is Jim'

最后一个字符串应该是“Hello\,我的名字是Jim”

Also, if anyone had an explanation as to why this works everywhere except the node console that'd be good!

另外,如果有人能解释一下为什么除了节点控制台以外,这个功能在任何地方都可以工作,那就更好了!

Many thanks

非常感谢

2 个解决方案

#1


3  

That behaviour is explained here:

这里解释这种行为:

By default, repl.REPLServer instances format output using the util.inspect() method before writing the output to the provided Writable stream (process.stdout by default).

默认情况下,repl。REPLServer实例在将输出写入提供的可写流(流程)之前,使用util.inspect()方法格式化输出。stdout默认情况下)。

In other words, this is what's happening "under the hood":

换句话说,这就是“引擎盖下”正在发生的事情:

const { inspect } = require('util')

let sentence = 'Hello, my name is Jim'

console.log( inspect( sentence.replace(/,/g, '\\,') ) );

inspect escapes strings, so \ becomes \\ (and the string also gets surrounded by quotation marks).

检查转义字符串,因此\成为\\(字符串也被引号包围)。

You can work around that by outputting the string yourself from the REPL:

你可以通过自己从REPL中输出绳子来解决这个问题:

console.log( sentence.replace(/,/g, '\\,') );

#2


0  

I suspect both Chrome and Node.js work the same, they just print the result differently.

我怀疑Chrome和Node。js工作方式相同,只是输出结果不同。

When you just print the length of the string instead of the string itself, it is probably the same.

当你只是打印字符串的长度而不是字符串本身,它可能是相同的。

Node.js then prints the string in a format that is easy to copy & paste. It is a valid JavaScript string literal.

节点。然后js以易于复制和粘贴的格式打印字符串。它是一个有效的JavaScript字符串文字。

Chrome on the other hand prints all characters as they are, possibly marking the string start and end by using different colors. When you copy the string from the JavaScript console to the clipboard and then paste it somewhere, the backslash might double.

另一方面,Chrome会打印所有的字符,可能是通过使用不同的颜色来标记字符串的开始和结束。当您将字符串从JavaScript控制台复制到剪贴板,然后将其粘贴到某个地方时,反斜杠可能会加倍。

#1


3  

That behaviour is explained here:

这里解释这种行为:

By default, repl.REPLServer instances format output using the util.inspect() method before writing the output to the provided Writable stream (process.stdout by default).

默认情况下,repl。REPLServer实例在将输出写入提供的可写流(流程)之前,使用util.inspect()方法格式化输出。stdout默认情况下)。

In other words, this is what's happening "under the hood":

换句话说,这就是“引擎盖下”正在发生的事情:

const { inspect } = require('util')

let sentence = 'Hello, my name is Jim'

console.log( inspect( sentence.replace(/,/g, '\\,') ) );

inspect escapes strings, so \ becomes \\ (and the string also gets surrounded by quotation marks).

检查转义字符串,因此\成为\\(字符串也被引号包围)。

You can work around that by outputting the string yourself from the REPL:

你可以通过自己从REPL中输出绳子来解决这个问题:

console.log( sentence.replace(/,/g, '\\,') );

#2


0  

I suspect both Chrome and Node.js work the same, they just print the result differently.

我怀疑Chrome和Node。js工作方式相同,只是输出结果不同。

When you just print the length of the string instead of the string itself, it is probably the same.

当你只是打印字符串的长度而不是字符串本身,它可能是相同的。

Node.js then prints the string in a format that is easy to copy & paste. It is a valid JavaScript string literal.

节点。然后js以易于复制和粘贴的格式打印字符串。它是一个有效的JavaScript字符串文字。

Chrome on the other hand prints all characters as they are, possibly marking the string start and end by using different colors. When you copy the string from the JavaScript console to the clipboard and then paste it somewhere, the backslash might double.

另一方面,Chrome会打印所有的字符,可能是通过使用不同的颜色来标记字符串的开始和结束。当您将字符串从JavaScript控制台复制到剪贴板,然后将其粘贴到某个地方时,反斜杠可能会加倍。