在javascript中允许带有西班牙语正则表达式的字母数字? [英] Allow alphanumeric with spanish regex in javascript?

查看:73
本文介绍了在javascript中允许带有西班牙语正则表达式的字母数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 javascript 中尝试了以下代码,

Hi I tried the following code in javascript,

if(/[^0-9a-zñáéíóúü]/i.test(string))
{
    alert("oK WITH THE INPUT");
}
else
{
    alert("error");
}

但我得到的结果是 OK ,即使我在字符串中输入了 @ 字符.我不知道为什么.

But I am getting a result OK , even I entered the @ characters in my string. I dont why.

我的目标是允许使用字母数字(英语和西班牙语).

My aim is to allow alphanumeric(English and spanish).

请帮我解决这个问题.

提前致谢.

推荐答案

if(/^[0-9a-zñáéíóúü]+$/i.test(string))
{
    alert("oK WITH THE INPUT");
}
else
{
    alert("error");
}

  • 使用+量词使正则表达式匹配多个字符立>
  • 使用 ^$ 锚点使其匹配整个输入
  • 不要否定字符类(删除^)
    • Make the regex match more than one character with the + quantifier
    • Make it match the entire input with ^ and $ anchors
    • Don't negate the character class (remove the ^)
    • 这篇关于在javascript中允许带有西班牙语正则表达式的字母数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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