Tom Folk
Tom Folk

Reputation: 133

Regex match first letter of words, but not "and"

How would I write a regex that matches the first letter of each word (going to be capitalising them via PHP), but not the word "and".

So far I have:

/\b([a-z])/ig

This works for matching the first letter of words, but obviously not including anything yet for not matching where the word is "and".

Upvotes: 2

Views: 545

Answers (1)

anubhava
anubhava

Reputation: 785286

You can use a negative lookahead for this:

/\b(?!and\b)[a-z]/

Negative lookahead (?!and\b) before \b[a-z] will allow matching all words starting with lowercase English alphabet except and.

RegEx Demo

Upvotes: 5

Related Questions