javascript - 正規的截取匹配問題求助
習慣沉默
習慣沉默 2017-07-05 10:53:53
0
6
898
src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png

想截取從static開始的字串,請問正規該如何寫?感謝

也就是static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png

另外由於url前半段可能會變動,所以最好還是用正規的好

習慣沉默
習慣沉默

全部回覆(6)
小葫芦

雷雷

刘奇

split('static')[1] 這樣的嗎? 還是必須用正規?

学霸

str.slice(str.search(/static/));

phpcn_u1582

正規應該用 static.* 就可以,下面是參考代碼

const regex = /static.*/g;
const str = `src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
ringa_lee

'srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png'.split('static')[1]

曾经蜡笔没有小新
'src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png'.match(/static.*/)

// Output: 
[
  "static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png"
]

這個問題的亮點:

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!