亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式匹配精確路徑與兩個 /

正則表達式匹配精確路徑與兩個 /

DIEA 2021-10-07 10:44:22
我試圖用正則表達式匹配下面的路徑。# test-site could be anything like "shoe-site", "best-clock", etc./content/test-site當前的正則表達式規則(\/content\/{1}\S+)就足夠了。問題是它匹配整個路徑,例如/content/test-site/en_US/abc.html.我只需要匹配/content/test-site.要匹配的路徑示例:https://localhost:4502/content/test-site/en_US/book/city/sample-rooms/airport-inn/propertyCode.00000.html到目前為止我嘗試過的正則表達式;(\/content\/[a-z-]+)\/[a-z]{2}_[A-Z]{2}\/book(ing-path)?\/(sample-|sample-details)(.*).[0-9]{5}.*/content/test-site is optional- it might not present sometimes in url.我做錯了什么,我該如何解決?
查看完整描述

3 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

您可以使用否定字符類

^(?:[^\/]*\/){2}[^\/]*

http://img1.sycdn.imooc.com//615e5f230001224e04910189.jpg

const path = '/content/test-site/en_US/abc.html';


const desired = path.match(/^(?:[^\/]*\/){2}[^\/]*/)


console.log(desired[0])


查看完整回答
反對 回復 2021-10-07
?
蝴蝶不菲

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 /


查看完整回答
反對 回復 2021-10-07
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

正則表達式匹配任何字符期望/:


content\/[^\/]+

這是字符類。以脫字符開頭的字符類將匹配該類中沒有的任何內容。更多關于這一點。


因此,使用 javascript:


const url = '/content/test-site/en_US/abc.html';


const path = url.match(/content\/[^\/]+/)


console.log(path[0])


查看完整回答
反對 回復 2021-10-07
  • 3 回答
  • 0 關注
  • 280 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號