Menu ▼

Posted By :
Total Views : 11918

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="">
    <script src=""></script>
    <script language="javascript">
        function RemoveSingleQuotes() {
            var outputstr = $("#txtinput").val().replace(/'/g, '');
        function ReplaceSingleQuotes() {
            var outputstr = $("#txtinput").val().replace(/'/g, '"');
        <input type="text" id="txtinput" />&nbsp;&nbsp;
        <input type="button" value="Remove Single Quotes" onclick="RemoveSingleQuotes();" />&nbsp;&nbsp;
        <input type="button" value="Replace Single Quotes" onclick="ReplaceSingleQuotes();" />