Skip to content Skip to sidebar Skip to footer

Regular Expression To Detect Double Quoted Javascript Object Properties With Brackets

There are three ways to access JavaScript Object property. someObject.propertyName someObject['propertyName'] // with single quote ' someObject['propertyName'] // with double quot

Solution 1:

But I couldn't write regular expression for the properties of the form someObject["propertyName"]:

You can use this regex:

\bsomeObject\[\s*(['"])(.+?)\1\s*\]

RegEx Demo

Or to match any object:

\b\w+\[\s*(['"])(.+?)\1\s*\]

In C#, regex would be like

Regexregex=newRegex(@"\bsomeObject\[\s*(['""])(.+?)\1\s*]");

RegEx Breakup:

\b      # word boundary
\w+     # match any word
\[      # match opening [
\s*     # match 0 or more whitespaces
(['"])  # match ' or " and capture it in group #1
(.+?)   # match 0 or more any characters
\1      # back reference to group #1 i.e. match closing ' or "
\s*     # match 0 or more whitespaces
\]      # match closing ]

Post a Comment for "Regular Expression To Detect Double Quoted Javascript Object Properties With Brackets"