Validators.pattern issues

Hello everyone,

I’ve experiencing issues with reactive forms Validators.pattern evaluation.

I want a string field to match anything except {} with a pattern as follows: / ^[^{}]*$ /

The fact is that if I use the pattern as a regular expression literal inside the Validators.pattern it works as expected: Validators.pattern(/^[^{}]*$/)

Instead if it is used directly in the constructor, it just don’t validate anything: Validators.pattern(’^[^{}]*’)

I tried this with different regular expressions and seems to work well as long as it doesn’t contain an alphanumeric character.

Am I doing it wrong?
Can’t seem why is this happening.

Thank you very much in advance!

And absolutely nice job with this framework, thanks a lot!

Check out the source. What I think is happening is that when you pass a string, it bookends it with ^$ before building a RegExp out of it. Since your RegExp already has those, I’m guessing that’s getting in the way. If that’s true, you should be able to pass the string [^{}]* and get the results you are expecting.

Hello rapropos,

Thank you very much for your answer.

I’m afraid I miss to mention it in my first post, sorry for that.
I was first trying out with (’^[^{}]*$’) before I noticed it was adding ^ $ when it’s a string.
Then I changed and try as you said, but still it isn’t working as expected.

Hmm. Can you just go with the RegExp then? JavaScript’s quoting and escaping rules give me headaches.

Yes, it works nice if I use the RegExp directly /^[^{}]*$/
Also I read it seems to be better using RegExp directly when the pattern isn’t changing.

Just couldn’t figure out if I was doing something wrong when trying the other way, I think it may work aswell so maybe something is happening in there.

One always learn something :slight_smile:
Thank you very much for your help!