简体   繁体   English

将双引号替换为单引号?

[英]Replace double quotes into single quotes?

It may sounds dumb but I don't succeed in replacing every double quotes of a string into simple quotes. 听起来有些愚蠢,但是我没有成功将字符串的每个双引号替换为简单的引号。 Here is what I do: 这是我的工作:

const str = '1998: merger by absorption of Scac-Delmas-Vieljeux by Bolloré Technologies to become \"Bolloré\".';
console.log(str.replace(`"`, `'`));

And it ouputs : 它输出:

1998: merger by absorption of Scac-Delmas-Vieljeux by Bolloré Technologies to become 'Bolloré"

What's going on here. 这里发生了什么。 I tried many other solutions... It just doesn't work. 我尝试了许多其他解决方案...只是行不通。

Any ideas? 有任何想法吗?

With str.replace() only the first instance of a substring will be replaced. 使用str.replace()只会替换子字符串的第一个实例。 You can use a regular expression and add the global flag to replace all instances. 您可以使用正则表达式并添加全局标志来替换所有实例。

str.replace(regexp|substr, newSubstr|function) str.replace(regexp | substr,newSubstr | function)

substr (pattern) A String that is to be replaced by newSubStr . substr(模式)将由newSubStr替换的String It is treated as a verbatim string and is not interpreted as a regular expression. 它被视为逐字字符串,而不被解释为正则表达式。 Only the first occurrence will be replaced. 仅第一次出现的将被替换。

console.log(str.replace(/\"/g, `'`));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM