![](/img/trans.png)
[英]Regex: Match a character that is unmatched in a negated set only a certain number of times at the end of a string
[英]Regex match word after negated set
我目前正在嘗試將以下案例與Regex相匹配。
目前的正則表達式
\\.\\/[^/]\\satoms\\s\\/[^/]+\\/index\\.js
案例
// Should match
./atoms/someComponent/index.js
./molecules/someComponent/index.js
./organisms/someComponent/index.js
// Should not match
./atomsdsd/someComponent/index.js
./atosdfms/someComponent/index.js
./atomssss/someComponent/index.js
然而,沒有一個案例是匹配的,我做錯了什么?
希望這會幫助你。 你添加了一些額外的字符,讓你的正則表達式失敗。
正則表達式: \\.\\/(atoms|molecules|organisms)\\/[^\\/]+\\/index\\.js
1.
\\.\\/
這將匹配./
2.
(atoms|molecules|organisms)
這將匹配atoms
或molecules
或organisms
3.
\\/[^\\/]+\\/
這將匹配/
然后直到/
4.
index\\.js
這將匹配index.js
為什么不只是這個簡單的模式 ?
\.\/(atoms|molecules|organisms)\/.*?index\.js
請嘗試以下方法:
\.\/(atoms|molecules|organisms)\/[a-zA-Z]*\/index\.js
應使用反斜杠\\
來轉義正斜杠(和其他特殊字符)。
\\.\\/(atoms|molecules|organisms)\\/
嚴格匹配'.atoms /'或.molecules
或organisms
。 如果沒有括號,它將匹配部分字符串。 |
是一個交替運算符,它匹配左側的所有內容或右側的所有內容。 [a-zA-Z]*
將匹配任何長度的字符串和字符。 az
占小寫,而AZ
占大寫。 *
表示一個或多個字符。 根據什么字符可能是someCompenent
你可能需要考慮使用數字[a-zA-Z\\d]*
。 \\/index\\.js
將匹配'/index.js'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.