Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(?:([\p{Lu}0-9_]{2,})|(\p{Lu}[\p{L}0-9_$]+)|([\p{L}_$][\p{L}0-9_$]*))\b
/
mg

Description

Surprisingly short regex that matches valid JavaScript identifier names and captures it in a group according common JavaScript naming conventions, e.g:

myVariable generic name for most identifiers, uses camelCase

MyClass class names or names of other template-like items, uses UpperCamelCase or PascalCase

REGEX_101 global, user-defined (semantically) constant names, uses CAPS_CASE

101Regex doesn't match at all, JS names can't start with number

... etc. (see unit tests for more defs)

Submitted by Neel Yadav - 2 years ago