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

Was it helpful?

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 ?$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top