Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript正则表达式匹配字符串最后一次出现后的所有内容_Javascript_Regex - Fatal编程技术网

Javascript正则表达式匹配字符串最后一次出现后的所有内容

Javascript正则表达式匹配字符串最后一次出现后的所有内容,javascript,regex,Javascript,Regex,我试图在JavaScript中最后一次出现字符串之后(但不包括!)匹配所有内容 例如,搜索是: [quote="user1"]this is the first quote[/quote]\n[quote="user2"]this is the 2nd quote and some url https://www.google.com/[/quote]\nThis is all the text I\'m wirting about myself.\n\nLook at me ma. Javas

我试图在JavaScript中最后一次出现字符串之后(但不包括!)匹配所有内容

例如,搜索是:

[quote="user1"]this is the first quote[/quote]\n[quote="user2"]this is the 2nd quote and some url https://www.google.com/[/quote]\nThis is all the text I\'m wirting about myself.\n\nLook at me ma. Javascript.
编辑:我希望匹配最后一个报价块之后的所有内容。所以我试图在最后一次出现“quote]”之后匹配所有内容?Idk如果这是最好的解决方案,但这是我一直在尝试的

老实说,我对正则表达式的东西一窍不通。。以下是我一直在尝试的结果

regex = /(quote\].+)(.*)/ig; // Returns null 
regex = /.+((quote\]).+)$/ig // Returns null  
regex = /( .* (quote\]) .*)$/ig  // Returns null   
我制作了一个JSFIDLE供任何人在这里玩:


一个选项是将所有内容匹配到最后一个
[/quote]
,然后再获取后面的内容

这是因为
*
天生就是贪婪的,它会匹配每一个,直到最后一个
\[\/quote\]

根据您提供的字符串,这将是第一个捕获组匹配:

\nThis is all the text I\'m wirting about myself.\n\nLook at me ma. Javascript.
但是由于字符串包含新行,并且
与新行不匹配,因此可以使用
[\s\s]
代替
来匹配任何内容


您还可以避免使用正则表达式,并与
.slice()一起使用:


或者,也可以使用
.split()
,然后获取数组中的最后一个值:


我不明白你想配什么。您的字符串中最后出现的字符串是什么?对不起,您的权利,我正在尝试匹配最后一个引号之后的所有内容…那么为什么要使用正则表达式呢?使用带有lastIndexOf的子字符串。您可以,先生。。你是个英雄!出色的工作。非常感谢。希望用户能从谷歌上找到类似的东西,也谢谢你的回答。
\nThis is all the text I\'m wirting about myself.\n\nLook at me ma. Javascript.
/[\s\S]*\[\/quote\]([\s\S]*)$/i
var match = '[\/quote]';
var textAfterLastQuote = str.slice(str.lastIndexOf(match) + match.length);
document.getElementById('res').innerHTML = "Results: " + textAfterLastQuote;
var textAfterLastQuote = str.split('[\/quote]').pop();
document.getElementById('res').innerHTML = "Results: " + textAfterLastQuote;