简体   繁体   English

使用正则表达式在字符串中查找不匹配字符

[英]find mismatch character in the string with regular expression

I have to find and show to user any special character other than keyboard characters in text area when text entering/pasting the text, here below I explained what am try to do! 当文本输入/粘贴文本时,我必须找到并向用户显示文本区域中除键盘字符以外的任何特殊字符,下面我解释了我要做的事情! Please help me as soon as possible? 请尽快帮助我?

if ($(txtMessage).val().trim() != "") {
         var pattern = "^[a-zA-Z0-9~`!@#$%^&*()_+-={}|:;<>,.?\/']+$";
         for(var i=0;i<$(txtMessage).val().length;i++){
             var subStr=$(txtMessage).val().substring(0, i);
             if(!subStr.match(pattern)){
                 $('#customDailogBox')
                    .text(
                            "You have entered or paste "+subStr.substring(i, i)+"input  characters are not supported characters!!!..");
             }
         }

change your loop condition and the substring function 更改循环条件和子字符串函数

 for(var i=1;i=<$(txtMessage).val().length;i++){
         var subStr=$(txtMessage).val().substr(i,1);
         if(!subStr.match(pattern)){
             $('#customDailogBox').text("You have entered or paste "+subStr.substring(i, i)+"input  characters are not supported characters!!!..");
         }
}

or you can also use the regular expression to match the whole string once 或者您也可以使用正则表达式匹配整个字符串一次

var unmatched_characters = $(txtMessage).val().match(/[^a-zA-Z0-9~`!@#$%^&*()_+-={}|:;<>,.?\/']/g);
if (unmatched_characters.length>0){
     $('#customDailogBox').text("You have entered or paste some characters that are not supported !!!..");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM