简体   繁体   English

Javascript RegEx部分匹配

[英]Javascript RegEx partial match

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. 基本上,如果新值匹配,我允许输入字符,否则我会阻止它。

The problem is that no value will ever match, becase "1" is not a full match, and will not allow me to type it. 问题是没有任何值匹配,因为“1”不是完全匹配,并且不允许我输入它。

Is it any way of testing a partial match for a regEx in javascript? 是否有任何方法可以在javascript中测试regEx的部分匹配?

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

EDIT 编辑

\\d{3} was just an example. \\ d {3}只是一个例子。 I need to use an email regex or a phone regex as input restriction. 我需要使用电子邮件正则表达式或手机正则表达式作为输入限制。

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

EDIT 2 编辑2

I have a textBox plugin with input restriction based on a regular expression. 我有一个textBox插件,其输入限制基于正则表达式。

The regular expression can be anything, a hex color Regex, for example: (#){1}([a-fA-F0-9]){6} 正则表达式可以是任何东西,十六进制颜色正则表达式,例如:(#){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 "#". 例如,如果文本框为空,则第一个允许的字符为“#”。

But if i test "#" character against the regex, it will return "false", because "#" by itself is not valid. 但是如果我对正则表达式测试“#”字符,它将返回“false”,因为“#”本身无效。

/^(#){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]*)?)?$

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. 它没有考虑到那里的所有可能性,但是对于像aa@bb.dd这样的基本功能,它工作得很好,并且还有进一步改进它的空间。

You would be better off by using a library like maskedinput.js . 使用像maskedinput.js这样的库会更好。 You can then setup your text input like follows: 然后,您可以设置文本输入,如下所示:

jQuery(function($){
    $("#your_input").mask("999");
});

UPDATE UPDATE

you can use a validator for forms and preset specific types of fields to validate 您可以使用验证器表单和预设特定类型的字段进行验证

You'll want to use explicit "|" 你会想要使用明确的“|” partial matches. 部分匹配。 For your color matching example it's pretty simple, you just need to explicitly match an empty string partial 对于您的颜色匹配示例,它非常简单,您只需要显式匹配空字符串partial

/^(|#[a-f0-9]{0,6})$/i.test(inputStr)

For an email it's more complicated since there are more partial match combinations 对于电子邮件而言,由于存在更多部分匹配组合,因此它更复杂

/^(|\w+|\w+@|\w+@\w+|\w+@\w+\.|\w+@\w+\.\w+)$/.test(inputStr)

Note that you can't get away with something like /^(|\\w*@|...)$/ since that matches @blah.com which isn't a valid partial input. 请注意,您无法使用/^(|\\w*@|...)$/类的/^(|\\w*@|...)$/因为它匹配@blah.com ,这不是有效的部分输入。

You can specify a range in the expression so that it matches anything between one and three digits like so: 您可以在表达式中指定一个范围,以便它匹配一到三个数字之间的任何内容,如下所示:

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

Just provide a regex that allows for partial matches. 只提供允许部分匹配的正则表达式。 eg /^\\d{1,3}$/ 例如/^\\d{1,3}$/

根据你的上一次编辑,这应该工作:

/^#[a-fA-F0-9]{0,6}$/

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

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