Search code examples
javascriptregexcodemirror

Find text between the first two same characters of my choice with regex


Assume the following output

.

20198818-119903 | firmware-check | passed: host test-1000 is connected to a test with Version: 333 | |
20198818-119903 | other-test-123 | passed: host test-1000 is connected to a test with 333:
20198818-119903 | test4| passed :| host | is connected to a test with 333
20198818-119903 | something | passed: host test-1000 is connected to a test with Version:

I want to extract firmware-check, other-test-123, test4, and something

So basically everything between the first two vertical bars.

I tried to solve this problem with txt2re but didn't work how I wanted (eg doesn't ignore host on the third line). I have never worked with regex and don't want to learn it just for this particular case. Can someone help me, please?


Solution

  • You can use this

    ^[^|]+\|([^|]+)
    

    enter image description here

    let str = `20198818-119903 | firmware-check | passed: host test-1000 is connected to a test with Version: 333 | |
    20198818-119903 | other-test-123 | passed: host test-1000 is connected to a test with 333:
    20198818-119903 | test4| passed :| host | is connected to a test with 333
    20198818-119903 | something | passed: host test-1000 is connected to a test with Version:`
    
    let op = str.match(/^[^|]+\|([^|]+)/gm).map(m=> m.split('|')[1].trim())
    
    console.log(op)