简体   繁体   中英

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}"); 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM