Match non printable/non ascii characters and remove from text

后端 未结 4 2099
别跟我提以往
别跟我提以往 2020-12-04 22:30

My JavaScript is quite rusty so any help with this would be great. I have a requirement to detect non printable characters (control characters like SOH, BS etc) as well exte

4条回答
  •  无人及你
    2020-12-04 22:42

    You have to assign a pattern (instead of string) into isNonAscii variable, then use test() to check if it matches. test() returns true or false.

    $(document).ready(function() {
        $('.jsTextArea').blur(function() {
            var pattern = /[^\000-\031]+/gi;
            var val = $(this).val();
            if (pattern.test(val)) {
                alert("It matched");
            }
            else {
                alert("It did NOT match");
            }
        });
    });
    

    Check jsFiddle

提交回复
热议问题