从范围中排除某些字符--javascript正则表达式 [英] Excluding some character from a range - javascript regular expression

查看:411
本文介绍了从范围中排除某些字符--javascript正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要验证单词最简单的正则表达式(我认为)

To validate only word simplest regex would be (I think)

/^\w+$/

我想从中排除数字和 _ (因为它现在接受 aa10aa 和aa_aa,我想拒绝他们。)

I want to exclude digits and _ from this (as it accept aa10aaand aa_aa now, I want to reject them)

我认为可以通过

I think it can be gained by

 /^[a-zA-z]+$/

这意味着我必须采取与前一种不同的方法。

which means I have to take a different approach other than the previous one.

但如果我想要排除任何此范围内的字符
假设我不允许 k K p P 或更多。

but what if I want to exclude any character from this range suppose I will not allow k,K,p,P or more.

有没有办法在范围内添加排除列表而不更改范围。

推荐答案

从<$ c中排除 k p $ c> [a-zA-Z] 你需要使用负前瞻断言。

To exclude k or p from [a-zA-Z] you need to use a negative lookahead assertion.

(?![kpKP])[a-zA-Z]+

必要时使用锚点。

^(?:(?![kpKP])[a-zA-Z])+$

它检查不是 k p 匹配每个字符之前。

It checks for not of k or p before matching each character.

OR

^(?!.*[kpKP])[a-zA-Z]+$

它只排除包含 k p 的行,并且只匹配那些只包含<$ c以外的字母的行$ c> k 或 p

It just excludes the lines which contains k or p and matches only those lines which contains only alphabets other than k or p.

DEMO

这篇关于从范围中排除某些字符--javascript正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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