regex - search for pattern that starts with alphabetics and ends with alphabets or space
-
24-05-2021 - |
Question
What is the correct regex for getting a string that contains only letters, must start with letters and a continuous string of letters. But can end with letters OR a space (just space and not tabs or returns).
I have this pattern /^\S*[a-zA-Z]\s*$/
Is it correct? do I need the \S*
at the start and how do I ensure that there is no spaces in between letters?
TIA
Solution
if it must start with letters, contain only letters (no spaces), and could end with a single space i think it should be
^[a-zA-Z]+ ?$
OTHER TIPS
^[a-zA-Z]+?[a-zA-Z ]$
This assumes a modern regular expression processor which allows for non-greedy (+?
) repeats.
/^[a-zA-Z]+[a-zA-Z ]$/
No need for \S*
And you can test it online. There are websites such as http://www.solmetra.com/scripts/regex/ to check regex.
To be unicode compatible:
^\pL ?$