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,'"');


Example:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="http://code.jquery.com/jquery-1.10.2.js"></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>