[英]Can someone please explain this regex? ^(?=.*prodCode=).*$
有人可以解釋一下此正則表達式嗎?
^(?=.*prodCode=).*$
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
prodCode= 'prodCode='
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the string
編輯
由於問題文本中的正則表達式已更改,因此解釋中的倒數第二行將變為:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
從行searhinng位置的開頭開始, prodCode=
之前的任何符號。 (?=)
表示僅檢查位置不匹配。 因此,在您的情況下,如果該行中存在像any symbol + prodCode=
這樣的字符串,則如果不匹配則匹配整行,然后返回false。
如果字符串中的任何地方都有prodCode=
,則此方法匹配,並且匹配完整的字符串。
編寫它的另一種方式(粗略地講,將方法返回值濫用為regex matcch)是
if (s.indexOf("prodCode=") != -1)
return s;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.