正则表达式禁止所有特殊字符但允许jQuery中的德语变音符号? [英] Regex to disallow all Special Chars but allow German Umlauts in jQuery?

查看:98
本文介绍了正则表达式禁止所有特殊字符但允许jQuery中的德语变音符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许所有字母数字字符并禁止RegEx中的所有特殊字符。但是我想允许德国的变形金刚,但因为它们也是特殊的Chars,我不能输入它们。我使用这个脚本:

I would like to allow all Alphanumeric Characters and disallow all Special Characters in a RegEx. But I would like to allow German Umlauts but becouse they are Special Chars too, I can't type them in. I use this Script:

    if(website_media_description.match(/[^a-zA-Z0-9]/g)) { 

    alert('Found Special Char');        

}

所以当一个äöüÄÖÜß在变量中而不是我得到的警惕。我也尝试过这个脚本:

So when a äöüÄÖÜß is in the variable than I get the alert too. I also tryed this Script:

    if(website_media_description.match(/[^a-zA-Z0-9äöüÄÖÜß]/g)) { 

    alert('Found Special Char');        

}

但这也行不通。有人可以告诉我我做错了吗?

But this also does not work. Can someone please tell me what I am doing wrong?

谢谢:)

推荐答案


我的测试字符串来自输入字段,我写描述测试1öäüÖÄÜß

my test String comes from an input field, i write "description test 1 öäüÖÄÜß"

你的问题来自于你没有考虑白名单中你想要的每个角色。

Your problem is coming from the fact you haven't considered every character you want in your whitelist.

让我们考虑你的测试字符串实际匹配的内容

Let's consider what is actually matched by your test string

"description test 1 öäüÖÄÜß".match(/[^a-zA-Z0-9äöüÄÖÜß]/g);
// [" ", " ", " "]

我们可以看到,它匹配3次,每次都是空白。因此,解决方案是为您的白名单添加一个空格(假设您不想允许标签/返回等)。

As we can see, it matched 3 times, and each time was whitespace. So, the solution is to add a space to your whitelist (assuming you don't want to allow tab/return etc).

"description test 1 öäüÖÄÜß".match(/[^a-zA-Z0-9äöüÄÖÜß ]/g);
// null

您的测试字符串现在通过 RegExp 没有匹配,这意味着它在这种情况下有效。

Your test string now passes the RegExp without a match, which means it is valid in this case.

这篇关于正则表达式禁止所有特殊字符但允许jQuery中的德语变音符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆