Home > Software design >  How can tweak this camelCase regex to allow for acronyms?
How can tweak this camelCase regex to allow for acronyms?

Time:10-30

edit: strictly speaking, they are not acronyms but you get what I mean.

I have found this resource very hekpful, https://newbedev.com/regex-for-pascalcased-words-aka-camelcased-with-leading-uppercase-letter but what if I need to tweak the lower camel case version to allow a set of initials to be used in the middle of a name. For example, the author provides a regex for lower camel case, [a-z] ((\d)|([A-Z0-9][a-z0-9] ))*([A-Z])?, which would match the following words:

xmlHttpRequest
newCustomerId
innerStopwatch
supportsIpv6OnIos
youTubeImporter
youtubeImporter
affine3D

but what about;

xmlHTTPRequest

where the capialized string features in the middle of the name?

CodePudding user response:

I would use this version, which allows for multiple capital letters:

[a-z0-9] (?:[A-Z0-9] [a-z0-9]*)*

Here is a demo showing that the pattern is working for you new edge case.

CodePudding user response:

If i understood you correct you just have to add a after [A-Z0-9] like this:

[a-z] ((\d)|([A-Z0-9] [a-z0-9] ))*([A-Z])?

https://regex101.com/r/ONgcrA/1

  • Related