简体   繁体   English

如何使用JavaScript检测当前正则表达式中允许的字符?

[英]How to detect what allowed character in current Regular Expression by using JavaScript?

In my web application, I create some framework that use to bind model data to control on page. 在我的Web应用程序中,我创建了一些框架,该框架用于将模型数据绑定到页面上的控件。 Each model property has some rule like string length, not null and regular expression. 每个模型属性都有一些规则,例如字符串长度,非null和正则表达式。 Before submit page, framework validate any binded control with defined rules. 在提交页面之前,框架将使用定义的规则来验证任何绑定的控件。

So, I want to detect what character that is allowed in each regular expression rule like the following example. 因此,我想检测每个正则表达式规则中允许的字符,如以下示例所示。

"^[0-9]+$" allow only digit characters like 1, 2, 3.
"^[a-zA-Z_][a-zA-Z_\-0-9]+$" allow only a-z, - and _ characters

However, this function should not care about grouping, positioning of allowed character. 但是,此功能不应在乎允许字符的分组,定位。 It just tells about possible characters only. 它仅说明可能的字符。

Do you have any idea for creating this function? 您有创建此功能的想法吗?

PS. PS。 I know it easy to create specified function like numeric only for allowing only digit characters. 我知道很容易创建仅允许数字字符的数字等指定函数。 But I need share/reuse same piece of code both data tier(contains all model validator) and UI tier without modify anything. 但是我需要共享/重用数据层(包含所有模型验证器)和UI层的同一段代码,而无需进行任何修改。

Thanks 谢谢

I must admit that I'm struggling to parse your question. 我必须承认,我正在努力分析您的问题。

If you are looking for a regular expression that will match only if a string consists entirely of a certain collection of characters, regardless of their order, then your examples of character classes were quite close already. 如果您要寻找一个仅在字符串完全由某个字符集合组成的情况下才匹配的正则表达式,而不管它们的顺序如何,那么您的字符类示例已经非常接近了。

For instance, ^[A-Za-z0-9]+$ will only allow strings that consist of letters A through Z (upper and lower case) and numbers, in any order, and of any length. 例如, ^[A-Za-z0-9]+$将只允许包含字母A至Z(大写和小写)和数字(以任何顺序和任何长度)的字符串。

You can't solve this for the general case. 对于一般情况,您无法解决此问题。 Regexps don't generally 'fail' at a particular character, they just get to a point where they can't match any more, and have to backtrack to try another method of matching. 正则表达式通常不会在某个特定字符上“失败”,它们只是到了无法再匹配的地步,不得不回溯以尝试另一种匹配方法。

One could make a regex implementation that remembered which was the farthest it managed to match before backtracking, but most implementations don't do that, including JavaScript's. 一个人可以制作一个正则表达式实现,该实现可以记住回溯之前它设法匹配的最远距离,但是大多数实现都不能做到这一点,包括JavaScript。

A possible way forward would be to match first against ^pattern$ , and if that failed match against ^pattern without the end-anchor. 一种可能的前进方式是先与^pattern$匹配,如果与^pattern匹配失败而没有末尾锚。 This would be more likely to give you some sort of match of the left hand part of the string, so you could count how many characters were in the match, and say the following character was 'invalid'. 这更有可能使您对字符串的左手部分进行某种匹配,因此您可以计算出匹配中有多少个字符,并说下一个字符“无效”。 For more complicated regexps this would be misleading, but it would certainly work for the simple cases like [a-zA-Z0-9_]+ . 对于更复杂的正则表达式,这会产生误导,但是对于像[a-zA-Z0-9_]+这样的简单情况,它肯定会起作用。

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

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