abarr
abarr

Reputation: 1140

regex for single lower case word

I am looking for a regex pattern that ensures the user puts in a single lower case word with only letters of the alphabet. Basically they are picking a subdomain. Thanks in advance

Upvotes: 6

Views: 10874

Answers (4)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626920

If you want to find all occurrences of lowercase-only ASCII-char words, you can use

text.match(/\b[a-z]+\b/g)

See the regex demo.

Details:

  • \b - a word boundary
  • [a-z]+ - one or more (+) lowercase ASCII letters
  • \b - a word boundary

The g flag makes it extract all occurrences.

See the JavaScript demo:

const text = "123456789 Ticket number (CO2) text";
console.log(text.match(/\b[a-z]+\b/g));

Upvotes: 0

Alex V
Alex V

Reputation: 18306

/^[a-z]+$/

make sure you aren't using 'i' after the last slash

/[a-z]+/

if you are searching for any words within the context

Upvotes: 0

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

^[a-z]+$ Will find one and only one lower-case word, with no spaces before or after the word.

Upvotes: 3

Gumbo
Gumbo

Reputation: 655309

The character class [a-z] describes one single character of the alphabet of lowercase letters az. If you want if an input does only contain characters of that class, use this:

^[a-z]+$

^ and $ mark the start and end of the string respectively. And the quantifier + allows one or more repetitions of the preceding expression.

Upvotes: 9

Related Questions