GANI
GANI

Reputation: 2059

the multiple spacing between the words with Reg Expression

I have this Reg exp

[a-zA-Z]+(([\'\,\.\-\s][a-zA-Z])?[a-zA-Z]*)*$

It allows one space between the words, how can I make this reg exp to accept multiple spaces between the words and spaces after the last word and before the first word.If the user's enters the text with multiple blank spaces how can I trim it to one blank space.

Upvotes: 0

Views: 168

Answers (2)

pstr
pstr

Reputation: 384

Try this regexp: [a-zA-Z]+((\s*[',.-]?\s*[a-zA-Z])?[a-zA-Z]*)*$

Upvotes: 1

Ry-
Ry-

Reputation: 225164

Replace all \s+ with a single space before matching. That handles the trimming and matching at the same time, for no extra cost.

Upvotes: 1

Related Questions