Match non printable/non ascii characters and remove from text

后端 未结 4 2098
别跟我提以往
别跟我提以往 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:34

    No need to test, you can directly process the text box content:

    textBoxContent = textBoxContent.replace(/[^\x20-\x7E]+/g, '');
    

    where the range \x20-\x7E covers the printable part of the ascii table.

    Example with your code:

    $('.jsTextArea').blur(function() {
        this.value = this.value.replace(/[^\x20-\x7E]+/g, '');
    });
    

提交回复
热议问题