-1

Looking for a regexp which match with characters starting by an uppercase and finish by lowercase or a number For example :

Foo2B => Foo, 2, B

I find ^([A-Z][a-z]*|[0-9]*) but it return Foo, 2B

2 Answers2

0

The | character means "OR", so your regex is actually matching two different things - either [A-Z][a-z]* OR [0-9]* at the beginning of a string.

What you're looking for is this: ^[A-Z][a-z0-9]*. This matches a leading uppercase letter, followed by a lowercase letter or number repeated zero or more times.

Kryten
  • 13,116
  • 3
  • 41
  • 61
-1

The regexp was OK I didn't used it well in JavaScript that's why I though the regexp was wrong.

For information

'Foo2B'.split(/([A-Z][a-z]*|[0-9]*)/).filter((elem) => elem !== '')
  • 1
    Never use `.split()` if you mean `.match()`. When extracting text, use `match` or `matchAll`. Use `'Foo2B'.match(/[A-Z][a-z]*|[0-9]+/g)` – Wiktor Stribiżew Aug 27 '20 at 18:57