question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question `@Matches` decorator with my regex shows true sometimes while false sometimes

See original GitHub issue

I am using class-validator with nestjs. I have a password field like this:

  @Matches(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).{6,64}$/gm, {
    message:
      'Password must be between 6 and 64 characters long with 1 special character and capital character each',
  })

but sometimes the password passes this while sometimes it doesnt

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

55reactions
aozisikcommented, Mar 6, 2020

@windok , @rubiin Please read the following thread on the use of g flag. It makes the regex object stateful, which explains this bug.

I guess you’ll have to avoid that flag to fix the issue.

https://stackoverflow.com/questions/6739136/consecutive-calls-to-regexp-test-fail-for-pattern-with-global-option

3reactions
kazemimorteza68commented, Jan 22, 2022

I have the same problem

@IsString()
 @MinLength(8)
 @MaxLength(20)
 @Matches(/((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&])[A-Za-z0-9@$!%*?&]+)+/g)
 password: string;

@Matches(‘^09[0-9]{9}’,“”,{“message”:“phone is incorrect”}) phone: string;

after regex pattern we need pass empty string and third argument be our message

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does this regex match true and false? - Stack Overflow
1 Answer 1 ... The "preceding pattern" is everything preceding the | , within the current group. So, true is the preceding pattern,...
Read more >
12. Regular Expressions | Advanced - Python Courses eu
This introduction will explain the theoretical aspects of regular expressions and will show you how to use them in Python scripts. The term...
Read more >
Python Regex: re.search() VS re.findall() - GeeksforGeeks
re.search() method either returns None (if the pattern doesn't match), or a re.MatchObject that contains information about the matching part of ...
Read more >
Regular Expressions :: Eloquent JavaScript
A regular expression consisting of only nonspecial characters simply represents that sequence of characters.
Read more >
What you can generate and how - Hypothesis! - Read the Docs
Examples from this strategy will shrink towards False (i.e. shrinking will replace True with False where possible). hypothesis.strategies.builds(target, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found