簡體   English   中英

JavaScript正則表達式匹配[[quote] [/ quote]]

[英]JavaScript regex match [[quote][/quote]]

var quote = 'some text here [[quote=bob]This is some text bob wrote[/quote]] other text here';

我正在嘗試獲取[[quote=bob]This is some text bob wrote[/quote]]

我正在使用: match(/[[quote=(.*?)](.*?)[/quote]]/)[1]

但這給了我some text here [[quote=bob]This is some text bob wrote[/quot

嘗試這個:

var quote = 'some text here [[quote=bob]This is some text bob wrote[/quote]] other text here';

console.log( quote.match(/(\[\[quote=(.*?)\](.*?)\[\/quote\]\])/) );
// [1] => "[[quote=bob]This is some text bob wrote[/quote]]"
// [2] => "bob"
// [3] => "This is some text bob wrote"

這里的問題是[是正則表達式中的保留字符,因此您必須對其進行轉義以將其用作“常規”字符。

這是您的一個開始,它將與您的可變報價中的[quote = bob]相匹配。

quote.match(/\\[quote=[az]*\\]/)

這是完整,正確和安全的版本。

string.match(/\[quote=[a-z]*\]([^\[]*)\[\/quote\]/)

它將返回適當的字符串,包括周圍的[quote]標簽作為第一個結果,僅返回內部字符串作為第二個結果。

我還使用了[az]字符類,因為您不想在=字符后匹配任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM