Replace single quote using javascript

This example will shows you how to replace single quote form a string. Default javascript replace method will replace the first occurrence of the single quote, to replace all the occurrences we have to use regular expression.

  • Removing all the single quotes from a string.
    var outputstr= inputstring.replace(/'/g,'');

  • Replacing all the single quotes with double quote in a string.
    var outputstr= inputstring.replace(/'/g,'"');

<!DOCTYPE html> <html xmlns=""> <head> <title></title> <script src=""></script> <script language="javascript"> function RemoveSingleQuotes() { var outputstr = $("#txtinput").val().replace(/'/g, ''); alert(outputstr); } function ReplaceSingleQuotes() { var outputstr = $("#txtinput").val().replace(/'/g, '"'); alert(outputstr); } </script> </head> <body> <div> <input type="text" id="txtinput" />   <input type="button" value="Remove Single Quotes" onclick="RemoveSingleQuotes();" />   <input type="button" value="Replace Single Quotes" onclick="ReplaceSingleQuotes();" /> </div> </body> </html>