1

I am trying to match a password with "no spaces or no symbols" in javascript regex.

What I tried was: /[^a-zA-Z0-9]|(.*\s)/g

I'm not sure if this is a correct regex. Can someone help please?

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
Sri
  • 1,205
  • 2
  • 21
  • 52
  • Your expression reads as "match a single non-alphanumeric character or match anything followed by whitespace." I don't think that's what you want. – Mike Cluck Oct 09 '17 at 20:06
  • Please provide example strings and expected output. – Wiktor Stribiżew Oct 09 '17 at 20:07
  • Is [**`^(?:[^@!$\n]+|\S+)$`**](https://regex101.com/r/a0zzeR/1) what you're looking for? – Jan Oct 09 '17 at 20:10
  • @sumeetkumar That regular expressions reads as "At the beginning of the string, find zero or more alphanumeric characters and/or underscores. Then, find zero or more alphanumeric characters and/or underscores at the end of the string." As an expression, it's non-sensical. – Mike Cluck Oct 09 '17 at 20:10
  • @MikeC Actually, the [`[A-z]` matches more than just letters](https://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret/29771926#29771926). – Wiktor Stribiżew Oct 09 '17 at 20:16
  • @WiktorStribiżew Ahhh, interesting. Good to know. Still, what they wrote is still non-sensical. – Mike Cluck Oct 09 '17 at 20:17
  • @MikeC Neither is the question in its current form. There is no sense matching a string that has no spaces, or a string that can be all spaces. It is the same as matching any string. – Wiktor Stribiżew Oct 09 '17 at 20:18
  • 1
    If you're unsure about the behavior of your regex, the best is to test it. There is some useful online tools to help you understand it like [regex101](https://regex101.com/) – Tim Oct 09 '17 at 20:20
  • What exactly do you mean by "symbol"? – Bergi Oct 09 '17 at 20:44

1 Answers1

2

https://regex101.com/r/XKJcTs/1

^[^\W_]+$

Reject everything that is not alphanumeric and the underscore. Require at least once. This will allow passwords with one character. You can change the + to {min, max} or to {min,} for no upper limit.

 ^[^\W_]{3,48}$

https://regex101.com/r/XKJcTs/2

Taken from this answer https://stackoverflow.com/a/23817519/2604378

voger
  • 666
  • 6
  • 22