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
The character class [a-z]
describes one single character of the alphabet of lowercase letters a
–z
. 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.
^[a-z]+$ Will find one and only one lower-case word, with no spaces before or after the word.
/^[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
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));
精彩评论