Reputation: 71
I need to construct regex to validate the text it can contain white space and hyphen with no numbers and special character except hyphen. Text should start with alphabets. No starting and ending with white space and hyphen. Between the words single white space should be allowed.
Ex text:
Upvotes: 0
Views: 57
Reputation: 26557
This should do the trick.
str.match(/^[a-z](?:([a-z-](?!--))+\s?(?!$))*(?:[a-z]*)$/)
That should give you a string that starts with a letter, then contains 1 or more letters or hyphens, followed by 1 optional space and then more words. Also does not allow duplicate spaces. It appears to work for all the test cases I could think of:
JSFiddle: http://jsfiddle.net/tA4X9/1/
Upvotes: 1