2

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".

Tom Folk
  • 123
  • 7
  • In PHP PCRE, you can use [`'~\band\b(*SKIP)(*F)|\b\p{Ll}~u'`](https://regex101.com/r/zO6xN6/1), too (added `/u` just in case Unicode letters are to be handled, too). – Wiktor Stribiżew Apr 22 '16 at 08:05

1 Answers1

5

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

anubhava
  • 761,203
  • 64
  • 569
  • 643