我需要從github gist url獲取兩個部分(用戶名/gistid和文件名)。索法爾我得到了這個: const gisturl = "https://gist.github.com/vanaf1979/329abed564c2ba10eda29a53b399ac69#file-parse-wp-rest-api-1-parse-js"; const myRegexp = /\.com\/+(.*)?#file-(.*)/gi; const matches = myRegexp.exec(gisturl); console.log(matches);惠特克給我0: ".com/vanaf1979/329abed564c2ba10eda29a53b399ac69#file-parse-wp-rest-api-1-parse-js"1: "vanaf1979/329abed564c2ba10eda29a53b399ac69"2: "parse-wp-rest-api-1-parse-js"到目前為止,一切都很好,除了文件名(#file-parse-wp-rest-api-1-parse-js)是可選的,如果我從字符串中刪除它,正則表達式doest將不再匹配。所以我需要正則表達式來匹配有或沒有文件名的兩種情況。任何幫助將不勝感激。
正則表達式將 giturl 與可選文件名匹配
牛魔王的故事
2022-09-02 21:30:35