0

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?

1 Answer 1

2

Yes there is. Use a character class in regex.

myStr = myStr.replace(/[BC&!@01]/g,"");

But, your jquery is a bit crazy. Correct it ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.