Daniel
Daniel

Reputation: 635

javascript find 3rd occureance of a substring in a string

I am trying to extract the substring between 3rd occurance of '|' character and ';GTSet' string within string

For Example, if my string is "AP0|#c7477474-376c-abab-2990-918aac222213;L0|#0a4a23b12-125a-2ac2-3939-333aav111111|ABC xxx;pATeND|#222222ANCJ-VCVC-2262-737373-3838383";

I would like to extract "ABC xxx" from above string using javascript. I have tried following options

var str = "AP0|#c7477474-376c-abab-2990-918aac222213;L0|#0a4a23b12-125a-2ac2-3939-333aav111111|ABC xxx;pATeND|#222222ANCJ-VCVC-2262-737373-3838383";
         alert(str.match(/^|\;pATeND(.*)$/gm));
          //var n = str.search(";pATeND");
         //to get the 3rd occurance of | character
         //var m = str.search("s/\(.\{-}\z|\)\{3}");

Upvotes: 1

Views: 49

Answers (1)

anubhava
anubhava

Reputation: 784998

This lookahead regex should work:

/[^|;]+(?=;pATeND)/

RegEx Demo

Or if paTeND text is know known then grab the value after 3rd |:

^(?:[^|]*\|){3}([^|;]+)

and use captured group #1.

Demo 2

Upvotes: 1

Related Questions