Reputation: 109
i'm not good with regex. I need to remove specific characters from an input field on the field. Say I want to remove "B, C, &, !, @, 0, 1". I use this code:
$('.classInput).on('input', function () {
var myStr = $(this).val();
myStr = myStr.replace("B", "");
myStr = myStr.replace("C", "");
myStr = myStr.replace("&", "");
myStr = myStr.replace("!", "");
myStr = myStr.replace("@", "");
myStr = myStr.replace("0", "");
myStr = myStr.replace("1", "");
$(this).val(myStr.toUpperCase());
});
However I suspect there is a better way of doing this with a regex call?
Upvotes: 0
Views: 1581
Reputation: 59232
Yes there is. Use a character class in regex.
myStr = myStr.replace(/[BC&!@01]/g,"");
But, your jquery is a bit crazy. Correct it ;)
Upvotes: 2