重要提示:我已經在 Stackoverflow 和其他地方查找過類似的問題,但找不到解決方案。
我正在嘗試制作一個與此模式之后的任何內容相匹配的正則運算式(該模式本身不包括在內)
[-a-zA-Z0-9] \/[-\._a-zA-Z0-9]

我嘗試將其^用作 NOT 運算子,如下所示:
[^([-a-zA-Z0-9] \/[-\._a-zA-Z0-9] )]
但它拋出一個語法錯誤:Unmatched '('. 似乎它將第一個]與第一個[而不是第二個相關聯。如何解決這個問題?
我也嘗試過像這樣做一個 Positive Lookbehind:
(?<=([-a-zA-Z0-9] \/[-\._a-zA-Z0-9] ).*)
但它不起作用。
我究竟做錯了什么?
uj5u.com熱心網友回復:
您可以在第 1 組中捕獲匹配之后的所有內容,并在替換中使用第 1 組。
[-a-zA-Z0-9] \/[-._a-zA-Z0-9] (. )
要測驗的檔案內容:
hello/w0rld/blbalba0
hel-lo/wor_ld?q=0
hello/w0rld)/blbalba0
hel-lo/wor_&ld?q=0(
正如您在要使用的評論中提到的sed:
sed -E 's/[-a-zA-Z0-9] \/[-._a-zA-Z0-9] (. )/\1/' file
輸出:
/blbalba0
?q=0
)/blbalba0
&ld?q=0(
uj5u.com熱心網友回復:

const pattern = new RegExp('([-a-zA-Z0-9] \/[-\._a-zA-Z0-9] )([\/\?].*)');
if(pattern.test('hello/w0rld/blbalba0')){
console.log('Matches string 1');
}
if(pattern.test('hel-lo/wor_ld?q=0')){
console.log('Matches string 2');
}
// Invalid string
if(pattern.test('hello/w0rld)/blbalba0')){
console.log('Matches string 3');
}
// Invalid string
if(pattern.test('hel-lo/wor_&ld?q=0')){
console.log('Matches string 4');
}
// if you wish to access the matches
const matches = pattern.exec('hel-lo/wor_ld?q=0');
console.log(matches[2]);
操場
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/361861.html
