简体   繁体   English

如何在 JavaScript 中用双引号替换单引号?

[英]How do I replace single quotes with double quotes in JavaScript?

The following code replaces only one single quote:以下代码仅替换一个单引号:

 var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]"; var b = a.replace("'", "\\""); console.log(b);

 var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]"; var b = a.replace(/'/g, '"'); console.log(b);

Edit: Removed \\ as there are useless here.编辑:删除 \\ 因为这里没有用。

Need to use regex for this:为此需要使用正则表达式:

var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
var b = a.replace(/\'/g, "\"");

http://jsfiddle.net/9b3K3/ http://jsfiddle.net/9b3K3/

You can use a global qualifier (a trailing g ) on a regular expression:您可以在正则表达式上使用全局限定符(尾随g ):

var b = a.replace(/'/g, '"');

Without the global qualifier, the regex ( /'/ ) only matches the first instance of ' .如果没有全局限定符,正则表达式 ( /'/ ) 只匹配'的第一个实例。

replaceAll(search, replaceWith) replaces ALL occurrences of search with replaceWith . replaceAll(search, replaceWith)替换所有出现的searchreplaceWith

Then, make sure you have a string by wrapping one type of qoutes by different type:然后,通过用不同类型包装一种类型的 qoutes 来确保你有一个字符串:

 "a 'b' c".replaceAll("'", '"')
 // result: "a "b" c"
    
 "a 'b' c".replaceAll(`'`, `"`)
 // result: "a "b" c"

More about replaceAll更多关于 replaceAll

replaceAll ( MDN ): replaceAll(search, replaceWith) replaceAll ( MDN ): replaceAll(search, replaceWith)

It's actually the same as using replace() with a global regex(*), merely replaceAll() is a bit more readable in my view.它实际上与将replace()与全局正则表达式 (* replace()一起使用相同,只是在我看来, replaceAll()更具可读性。

(*) Meaning it'll match all occurrences. (*) 意味着它将匹配所有出现。


Example 1 - search with a string示例 1 - 使用字符串搜索

const p = 'Please replace all 2020 occurrences with 2021. 2020. 2020.' 
   
console.log(p.replaceAll('2020', '2021'));
// Result: "Please replace all 2021 occurrences with 2021. 2021. 2021."

Example 2 - search with regex示例 2 - 使用正则表达式搜索

const p = 'Please replace all 2020 occurrences with 2021. 2020. 2020.'    
const regex = /2020/gi


console.log(p.replaceAll(regex, '2021'));
// Result: "Please replace all 2021 occurrences with 2021. 2021. 2021."

Important( ! ) if you choose regex:重要( )如果您选择正则表达式:

when using a regexp you have to set the global ("g") flag;使用正则regexp您必须设置全局 ("g") 标志; otherwise, it will throw a TypeError: "replaceAll must be called with a global RegExp".否则,它会抛出一个 TypeError:“replaceAll 必须用全局 RegExp 调用”。


You can also use a function as replaceWith您还可以使用函数作为replaceWith

In this case, the function will be invoked after the match has been performed.在这种情况下,将在执行匹配后调用该函数。 The function's result (return value) will be used as the replacement string.函数的结果(返回值)将用作替换字符串。

This looks suspiciously like bad JSON, so I suggest using actual array and object literals, then encoding the proper way:这看起来很像糟糕的 JSON,所以我建议使用实际的数组和对象文字,然后以正确的方式编码:

var a = [{'column1':'value0','column2':'value1','column3':'value2'}];
var b = JSON.stringify(a);

添加g修饰符: var b = a.replace(/'/g, '"');

您可以使用带有全局标志g的 RegExp 并且所有引号都将被替换:

var b = a.replace(/'/g, '"');

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

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