Alex Lowe
Alex Lowe

Reputation: 871

Match word(s) without title case in string - Regex

I am trying to match with RegEx any word in this sequence (ex: 1943 The brown Fox Jumped) that is a string that starts with numbers and then after that has words with spaces between them. I have spent hours trying to figure out how to match any word in that sequence that isn't title cased (eg. The Brown Fox Jumped). I have figured out how to match if all words aren't title cased but not if one or two are in the middle of a sentence. How would I go about creating a regular expression to detect if one or more words aren't title cased?

The pattern that I am working with currently is /(?<=^\d+\s)([a-z]+)/g. Here is a Regex101 demo of my last attempt. As mentioned earlier I figured out how to match if all of the words in the string weren't title cased as shown in this Regex101 demo. Any help would be greatly appreciated :)

Upvotes: 3

Views: 267

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627292

You can use an infinite-width lookbehind based regex solution in case you must do it with a regex:

/(?<=^\d+\s.*?)\b[a-z]+\b/gs

See the regex demo.

Details

  • (?<=^\d+\s.*?) - a positive lookbehind that matches a location that is immediately preceded with
    • ^ - start of string
    • \d+ - 1+ digits
    • \s - a whitespace]
    • .*? - any 0 or more chars as few as possible
  • \b[a-z]+\b - a whole word consisting of lowercase ASCII letters.

Note: this regex does not work in IE and older browsers that do not support the ECMAScript 2018+ standard.

Upvotes: 3

Yoni Ziv
Yoni Ziv

Reputation: 176

Try this

([A-Z])\w+

It matches all words with Capital letters in them.

This is actually the default example in Regex Generator. Test it here:

https://regexr.com/

enter image description here

Upvotes: -2

Related Questions