【发布时间】:2015-09-20 06:54:50
【问题描述】:
访问 JavaScript Object 属性的方法有三种。
someObject.propertyNamesomeObject['propertyName'] // with single quote '-
someObject["propertyName"] // with double quote "
括号之间允许有空格,即someObject[ 'propertyName' ] 或someObject[ "propertyName" ]。
为了检测文本文件中对象someObject 的所有属性,我编写了以下正则表达式。
Regex regex = new Regex(@"someObject\.[a-zA-Z_]+[a-zA-Z0-9_]*");检测someObject.propertyName形式的属性。regex = new Regex(@"someObject\[[ ]*'[a-zA-Z_]+[a-zA-Z0-9_]*'[ ]*\]");检测someObject['propertyName']形式的属性。
但我无法为someObject["propertyName"] 形式的属性编写正则表达式。每当我尝试在正则表达式 Visual Studio 中编写 " 或 \" 时都会出错。
我在互联网上找到了一些正则表达式来检测双引号文本。例如this。但我无法在正则表达式中添加\[ 和\],Visual Studio 给出错误。
如何检测someObject["propertyName"] 表单的属性?
我正在使用 C# System.Text.RegularExpressions 库。
【问题讨论】:
标签: javascript c# regex