null
null

Reputation: 479

Regex to require numbers, then a space, then letters?

Is it possible in regex (using javascript if that matters) to require numbers, then a space, then letters?

I'm not even sure where to begin..

I know this works if I was doing it in an HTML5 pattern...

pattern="[0-9]+[ ][a-zA-Z0-9]+"

But that's an HTML5 pattern, and I'm trying to figure out the regex equivalent.

Upvotes: 2

Views: 6320

Answers (1)

Fueled By Coffee
Fueled By Coffee

Reputation: 2569

This will work for you. It's exactly what you're trying to do just more concise.

/\d+\s[a-zA-Z0-9]+/gi

\d+ matches any digit one or more times
\s   matches any whitespace
[a-zA-Z0-9]+ matches any letters or numbers one or more times.

This is what your current RegEx is showing but if you want to truly only have letters like you specify:

Is it possible in regex (using javascript if that matters) to require numbers, then a space, then letters?

Use: /\d+\s[a-z]+/gi


The g and i modifiers state that the pattern should search globally and case insensitively

Example: https://regex101.com/r/8tmFY9/3

More info on Regular Expressions

Upvotes: 5

Related Questions