Reputation: 799
I have this string:
string with two substrings:
srt=60389052 - OLGA YANETH PARRA PINILLA8390507 - VICENTE ARROYAVE ARANGO
So, I Know how found the first substring (digit + words) and the second:
w1 = srt.match (/^\d+\D+)/); //matches = 60389052 - OLGA YANETH PARRA PINILLA
w2 = srt.match (/\d+\D+$/); //matches = 8390507 - VICENTE ARROYAVE ARANGO
that's fine but if the string are more longer I have problems to match the second substring, for example:
string with three substrings:
srt=60389052 - OLGA YANETH PARRA PINILLA8390507 - VICENTE ARROYAVE ARANGO777777 GERMAN CASTRO
I want to find this:
8390507 - VICENTE ARROYAVE ARANGO
The string sometimes have only two substrings and sometims have three so I have to find always the second one no matter if the string have 2 or 3 substrings, I mean If I use the w2 in the larger string don't match the substring that I want because would find the last substring into three but not the second one.
Upvotes: 1
Views: 160
Reputation: 25322
You can simply use the global flag:
console.log(srt.match(/\d+\D+/g))
Upvotes: 0
Reputation: 145378
Do global match and get the substring that you need by index (starting from 0
):
var matches = str.match(/\d+\D+/g);
console.log(matches[1]); // "8390507 - VICENTE ARROYAVE ARANGO"
Upvotes: 3
Reputation: 2365
I know the regex is not the most elegant, but since Javascript doesn't support lookbehind, this might not be so bad:
str.match(/^\d+\D+\d+\D+/)[0].replace(/^\d+\D+/,'');
Also, if you aren't intent on 'match()' - this also is nicer than above:
str.replace(/\d+\D+(\d+\D+).*/,'$1');
Cheers.
Upvotes: 1