Javascript RegEx部分匹配 [英] Javascript RegEx partial match

查看:54
本文介绍了Javascript RegEx部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式模式,可以验证三位数的数字

I have a regular expression pattern, which validates for a three digit number

/^\d{3}$/.test("123")   // true
/^\d{3}$/.test("123.")  // false

我想将此正则表达式用作文本框上的输入限制.

I want to use this regex as an input restriction on a textbox.

基本上,如果新值匹配,则允许输入字符,否则禁止输入.

Basically, if the new value matches, i allow the character to by typed, otherwise i prevent it.

问题在于,没有值会匹配,因为"1"不是完全匹配,并且不允许我键入该值.

The problem is that no value will ever match, becase "1" is not a full match, and will not allow me to type it.

有什么方法可以测试javascript中的regEx的部分匹配?

Is it any way of testing a partial match for a regEx in javascript?

/^\d{3}$/.test("123")   // true
/^\d{3}$/.test("12")    // "partial match"
/^\d{3}$/.test("a12")   // false

编辑

\ d {3}只是一个例子.我需要使用电子邮件正则表达式或电话正则表达式作为输入限制.

\d{3} was just an example. I need to use an email regex or a phone regex as input restriction.

"email"        // true
"email@"       // true
"email@@"      // false
"@yahoo.com"   // false

编辑2

我有一个textBox插件,具有基于正则表达式的输入限制.

I have a textBox plugin with input restriction based on a regular expression.

正则表达式可以是十六进制颜色的正则表达式,例如:(#){1}([[a-fA-F0-9]){6}

The regular expression can be anything, a hex color Regex, for example: (#){1}([a-fA-F0-9]){6}

我需要防止用户插入与正则表达式不匹配的字符.

I need to prevent user to insert characters which doesn't match the regex.

例如,如果文本框为空,则允许的第一个字符为#".

For example, if the textbox is empty, the first allowed character would be "#".

但是如果我针对正则表达式测试#"字符,它将返回"false",因为#"本身无效.

But if i test "#" character against the regex, it will return "false", because "#" by itself is not valid.

/^(#){1}([a-fA-F0-9]){6}$/.test("#") // false

但是同时,#"是部分有效的,因为它尊重正则表达式格式(我应该允许用户键入它)

But at the same time, "#" is partial valid because it respects the regex format (and i should allow user to type it)

我需要知道的是,是否可以验证字符串是否与正则表达式部分匹配,所以我可以允许用户键入字符.

What i need to know is if i can verify if a string is a partial match of a regex, so i can allow the user to type the character.

/^(#){1}([a-fA-F0-9]){6}$/.test("#")        // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#0")       // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#00")      // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#000")     // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#0000")    // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#00000")   // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#000000")  // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#000000D") // is not a match, prevent typing

推荐答案

您可以使用()?输入更多字母和/或字符来部分验证电子邮件地址.每个()?都进入验证树的深处.

You could partially validate the email address by using ()? for more letters and/or characters. Every ()? going deeper in the validation tree.

以下正则表达式模式逐个字母地验证电子邮件地址.

The following regular expression pattern validates email address letter by letter.

^[a-zA-Z]+(@{1}[a-zA-Z]*(\.{1}[a-zA-Z]*)?)?$

它没有考虑到所有可能性,但对于像aa@bb.dd这样的基本可能性来说,它工作得很好,并且还有进一步改进的空间.

It does not take into account every possibility out there, but for basic ones like aa@bb.dd it works just fine and there's room to improve it further.

这篇关于Javascript RegEx部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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