0

I have a lightning input component and I need the input values to be only alphanumeric characters, underscores and/or spaces. However, there is also one more requirement to have at least one number or letter in the string (lest there be a string composed entirely of underscores and/or white-spaces). For the first condition, I have found that the regexp is ^[\w ]+$. How do I modify this regexp such that I can also fulfill the second condition?

1
  • This question, being chiefly regex, might be a better fit on StackOverflow. My gut feeling, though, is that you can't fulfill both requirements in a single regexp. Multiple regex could probably do it.
    – Derek F
    Nov 25, 2019 at 15:00

2 Answers 2

5

You'd just have to specify that there's a requirement of at least one letter or number somewhere in the string. Here's a possible solution:

^(\w| )*[0-9A-Za-z](\w| )*$

This says we must match zero or more word characters (0-9, a-z, A-Z, and underscore) or a space, one letter or number, followed by zero or more word characters or a space.

1

I think you want you regex to look like this:

\w*\d

Where \w matches all word characters and the * matches the previous the condition and \d looks numeric numbers

1
  • This should be close, but the requirement is to also capture spaces, so I think: \w* *\d would cover that.
    – nbrown
    Nov 25, 2019 at 15:03

Not the answer you're looking for? Browse other questions tagged .