3 回答

TA貢獻1757條經驗 獲得超8個贊
您可以使用否定字符類
^(?:[^\/]*\/){2}[^\/]*
const path = '/content/test-site/en_US/abc.html';
const desired = path.match(/^(?:[^\/]*\/){2}[^\/]*/)
console.log(desired[0])

TA貢獻1810條經驗 獲得超4個贊
這是另一種方法:
(?:\/[a-zA-Z0-9-]+){2}
解釋:
(?: # Non-capturing group
\/[a-zA-Z0-9-]+ # Match starting with / and followed with characters/digits/-
) # Close grouping
{2} # Match two times, i.e. only match with two /

TA貢獻1836條經驗 獲得超13個贊
正則表達式匹配任何字符期望/:
content\/[^\/]+
這是字符類。以脫字符開頭的字符類將匹配該類中沒有的任何內容。更多關于這一點。
因此,使用 javascript:
const url = '/content/test-site/en_US/abc.html';
const path = url.match(/content\/[^\/]+/)
console.log(path[0])
添加回答
舉報