有三種訪問JavaScript Object屬性的方法。使用括號檢測雙引號javascript對象屬性的正則表達式
someObject.propertyName
someObject['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
庫。
當我嘗試在Visual Studio寫這些正則表達式,它給錯誤。 [dotnetfiddle](https://dotnetfiddle.net/7bo0bS)。 Visual Studio不編譯它。 –
嘗試:'正則表達式正則表達式=新正則表達式(@「\ bsomeObject \ [\ s *(['」「])(。+?)\ 1 \ s *]」);' – anubhava
Thanks:如果你向我解釋,我會很高興。爲什麼視覺工作室給錯誤?我在哪裏可以獲得適當的文檔。我從昨晚開始搜索它並無法解決它。 –