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
发布于 2014-11-07 18:19:49
试试这个:
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"发布于 2014-11-07 18:20:53
这里的问题是,[是正则表达式中的保留字符,因此您必须将它转义为“正则”字符。
下面是您的一个开始,这将与您的变量引号中的quote=bob匹配。
quote.match(/\[quote=[a-z]*\]/)
这是完整、正确和安全的版本。
string.match(/\[quote=[a-z]*\]([^\[]*)\[\/quote\]/)它将返回适当的字符串,包括周围的引号标记作为第一个结果,而只返回内部字符串作为第二个结果。
我还使用了a字符类,因为您不希望在=字符之后匹配任何内容。
https://stackoverflow.com/questions/26807458
复制相似问题