[英]Get last occurrence using RegEx
I have a huge string with this inside: 我里面有一个巨大的字符串:
linha[0] = '12/2010 281R$ 272.139,05 ';
linha[0] = '13SL 1R$ 226.185,81 ';
Both lines are separate, and I need get the last occurrence from both. 这两行是分开的,我需要从这两行中获得最后一次出现。 I'm using the following regex to match the first one:
我正在使用以下正则表达式来匹配第一个:
/linha\[0]\s=\s'(.*)';/
I would like to get the second "linha..." too, but I don't know exactly how. 我也想获得第二个“ linha ...”,但我不知道具体如何。
That's how i'm using this regex to get the first "linha...": 这就是我使用此正则表达式获取第一个“ linha ...”的方式:
string.match(/linha\[0]\s=\s'(.*)';/);
output: 输出:
linha[0] = '12/2010 281R$ 272.139,05 ';
Also, i can't do extra work, i need get the second occurrence using only regex. 另外,我不能做额外的工作,我只需要使用正则表达式就可以得到第二次出现。
If you want to get the last occurrence of your regex in a string (and assuming it exists), you can do 如果要在字符串中获取正则表达式的最后一次出现(并假设它存在),则可以执行
var str = hugeString.match(/linha\[0]\s=\s'([^']*)';/g).pop();
(yes, I changed .*
to [^']*
for a better efficiency, ignore that if you have quotes in your inner string) (是的,为了更好的效率,我将
.*
更改为[^']*
,如果您的内部字符串中带有引号,则忽略它)
Now, if you want to extract just the submatch, you can do 现在,如果您只想提取子匹配项,则可以
var regex = /linha\[0]\s=\s'([^']*)';/g,
arr,
str;
while (arr = regex.exec(hugeString)) str = arr[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.