jQuery Replace Single or Double Quotes

Sam Deering

Simple jQuery code snippets to replace single quotes and replace double quotes using the jQuery.replace function that takes two parameters. First the global search for all quotes (either single or double) and then a blank space to replace them (change the second parameter if you wish to replace them with something else – this code just removes them).

//replace all single quotes
var myStr = myStr.replace(/'/g, '');

//replace all double quotes
var myStr = myStr.replace(/"/g, '');

//or abit of fun, replace single quotes with double quotes
var myStr = myStr.replace(/'/g, '"');

//or vice versa, replace double quotes with single quotes
var myStr = myStr.replace(/"/g, ''');

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • Parveen Verma

    It’s a great post ….thanks

  • Kenneth M. Kolano

    Um, there is no JQuery here?