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

Posted by Soul_Master on Stack Overflow See other posts from Stack Overflow or by Soul_Master
Published on 2010-05-13T12:31:24Z Indexed on 2010/05/13 12:44 UTC
Read the original article Hit count: 211

Filed under:
|

In my web application, I create some framework that use to bind model data to control on page. Each model property has some rule like string length, not null and regular expression. 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. By the way, complex regular expression like find two words near(\bword1\W+(?:\w+\W+){1,6}?word2\b) must be ignore to verify and it should return any characters is possible.

Do you have any idea for creating this function?

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.

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex