简体   繁体   English

使用RegEx获取最后一次出现

[英]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.

相关问题 如何使用正则表达式javascript获取最后一次出现? - How to get last occurrence with regex javascript? 正则表达式:在第一次出现和最后一次出现之间获取 - Regex: Get between first occurrence of one and last occurrence of another 如何使用正则表达式匹配模式的最后一次出现 - How to match the last occurrence of a pattern using regex 如何使用Javascript正则表达式从可能单词的列表中获取单词的最后出现? - How to get the last occurrence of a word from a list of possible words using Javascript regex? 使用前瞻性获取javascript中模式的最后一次出现 - using a lookahead to get the last occurrence of a pattern in javascript 使用 RegExp 获取最后一次出现的字符串 - Get the last occurrence of a string using RegExp 正则表达式在字符串中最后一次出现“#”后获取整个字符串 - Regex to get the entire string after last occurrence of “#” in a string 使用正则表达式仅用JS替换最后一次出现的模式 - Using regex to replace only the last occurrence of a pattern with JS 在javascript中使用regex查找字符串中最后一次出现的逗号 - Find last occurrence of comma in a string using regex in javascript 使用正则表达式替换 email 字符串中倒数第二个字符(点) - Replace second last occurrence of a char(dot) in an email string using regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM