How to Replace Single with Double quotes in JavaScript-Code Examples

Radu TM

How to Replace Single with Double quotes in JavaScript

JavaScript by Radu TM June 21, 2022

// How to Replace Single with Double quotes in JavaScript
const str = "I'm a 'string' with quotes";

const newStr = str.replace(/'/g, '"');

console.log(newStr); // logs "I'm a "string" with quotes"

0

27.980

How to Replace Single with Double quotes in JavaScript

JavaScript by Radu TM June 21, 2022

const str = "I'm a 🐨";

// using .replace()
const newStr = str.replace(/'/g, '"');

console.log(newStr); // I"m a 🐨

0

27.980

How to Replace Double with Single Quotes in JavaScript

JavaScript by Radu TM June 21, 2022

// this is a comment
const input = "I'm using \"quotes\" in this string";

// we want to replace the double quotes with single quotes
// so we create a regex that matches double quotes
const regex = /"/g;

// and we use the .replace() method to replace them with single quotes
const output = input.replace(regex, "'");

// and now the output variable contains the string with single quotes
console.log(output); // I'm using 'quotes' in this string

0

18.564

How to Replace Double with Single Quotes in JavaScript

JavaScript by Radu TM June 21, 2022

const str = "How to Replace \"Double\" with Single Quotes in JavaScript";

// using regex
str.replace(/"/g, "'");

// using built-in methods
str.split('"').join("'");

0

18.564

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories