NewOldMax / react-form-validator-core

Core validator component for react forms
MIT License
95 stars 44 forks source link

Problem with RegEx flags #73

Closed jamesdefant closed 4 years ago

jamesdefant commented 4 years ago

Great library but I have run into a problem. Unless I am somehow writing my regex wrong, I cannot get flags ( such as /i - ignore lowercase ) to work properly. It seems that the forward slash is automatically escaped with a back-slash thus invalidating the regex.

This: ^[a-zA-Z]+$/i ends up as this: ^[a-zA-Z]+$\/i - invalid

I think a simple solution would be to allow multiple arguments for the regex as it's constructor signature is:

new RegExp(pattern : string, flags = '')

or

new RegExp(regExp : RegExp, flags = regExp.flags) this being the one you've implemented that inserts back-slashes

the validator could possible be written as such (with another colon as delimiter to pull in multiple arguments). Not certain what side effects this would have on your code:

validators={[ 'matchRegexp:^[a-zA-Z]+$:i' ]}

NewOldMax commented 4 years ago

@jamesdefant Hi such behavior will bring more complexity, because ':' symbol may be a part of regexp. So regex flag won't be supported you always can create your own validation rule to handle more complex cases