Home > Mobile >  How to check lowerCamelCase string with condition with regex
How to check lowerCamelCase string with condition with regex

Time:09-15

I am trying to make a regex for the endpoint which can validate a string for lowercase (or concatenated with - )

BUT if there is a curly braces, then a word inside a curly braces should be in lowerCamelCase.

Here is an example string:

/v1/accounts/{accountNumber}/balances
OR /v1/user-account/balances-sheet

I am using a following regex '^[\/a-z\{\}] $' to validate above examples but not getting the point how I can validate curly braces condition OR if it possible to combine in single Regex?

CodePudding user response:

You may use this regex to validate both cases:

^(?:\/\{[a-z] (?:[A-Z][a-z]*)*\}|\/\w (?:-\w )*) $

Regex 101

CodePudding user response:

Try this:

^(\/([\da-z] (-[\da-z] )?|\{[a-z] ([A-Z][a-z] )*})) $

See live demo.

  • Related