Ad Code

jQuery Replace Single or Double Quotes

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

1, textarea

$(document).on('change keyup blur', function(){
    var t = $('textarea'),
    v = t.val();
    v = v.replace( /[\'\"]/g, function(m){ return m === '"'? " " : ' '; });
    t.val(v);
    //console.debug(v);
});


2, input

$(document).on('change keyup blur', function(){
    var tt = $('input'),
    tv = tt.val();
    tv = tv.replace( /[\'\"]/g, function(m){ return m === '"'? " " : ' '; });
    tt.val(tv);
    //console.debug(v);
});

Post a Comment

0 Comments