在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  HTML/ 一個(gè)正則匹配奇怪的問(wèn)題?

一個(gè)正則匹配奇怪的問(wèn)題?

先上代碼:

if(/(\module\/(?:.+[^\.html]))/.test("./src/module/comment/comment.html")){
    console.log(RegExp.$1) //module/comment/commen
}

if(/(\module\/(?:.+[^\.js]))/.test("./src/module/comment/comment.js")){
    console.log(RegExp.$1) //module/comment/comment
}

從上面的輸出可以看到我只是將代碼中的html換成js輸出就不一樣了,按理說(shuō)這兩段代碼不都是應(yīng)該輸出module/comment/comment嗎,為何輸出就不一樣了?

下面這段代碼也是一樣的問(wèn)題:

if(/(\module\/(?:.+[^\.html]))/.test("./src/module/comment/details.html")){
    console.log(RegExp.$1) //module/comment/details
}

if(/(\module\/(?:.+[^\.js]))/.test("./src/module/comment/details.js")){
    console.log(RegExp.$1) //module/comment/detail
}

不都是應(yīng)該輸出/module/comment/details嗎?

回答
編輯回答
傲嬌范

[^]反向匹配
[^abc] 匹配非abc一個(gè)字符
/[^js]+/.test('as') 匹配到a 不會(huì)匹配s
[^\.html]匹配非 . h t m l 一個(gè)字符 比如a,b,e都通過(guò) t不通過(guò) 并不是匹配非.html這5個(gè)連起來(lái)的詞
所以你的第一個(gè)./src/module/comment/comment.html 匹配到了t所以匹配結(jié)束
其他的類(lèi)似

2018年8月29日 19:14