skip to Main Content

I have this type of text from a tag:

RP/HDQ1S/HDQ1SJFDEAK/C8HJ/2024837        20APR23/0339Z   GLS123
        DATA
    001 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E*
    002 OS/AF1439 Q 20APR 4 HAJCDG LK1 0600 0735/LK *1A/E*
    003 OS/AF9466 Q 20APR 4 CDGRNS LK1 1235 1340/LK *1A/E*
    004 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF
        DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133
        0Z

I’m trying to merge the lines like that:

RP/HDQ1S/HDQ1SJFDEAK/C8HJ/2024837        20APR23/0339Z   GLS123
        DATA
    000 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E*
    000 OS/AF1439 Q 20APR 4 HAJCDG LK1 0600 0735/LK *1A/E*
    000 OS/AF9466 Q 20APR 4 CDGRNS LK1 1235 1340/LK *1A/E*
    000 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJFDEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR1330Z

With the following expression, I managed to exclude the first line, containing "RP", but I’m only getting one match and I need to get two:

(^(?!RP).+)((n        )|(n      ))

I’m using it in JavaScript to replace like that:

text.textContent = text.textContent.replace(/^(?!RP)(.+)((n        )|(n      ))/gm, function (match, p1) { return p1 });

The goal is to merge every new line with the previous one if the new line start with 6 or 8 empty spaces, and to ignore the first line that starts with RP.

2

Answers


  1. You’ll have to run it multiple times.

    const regex = /(^(?!RP).+)(n        )/gm;
    
    // Alternative syntax using RegExp constructor
    // const regex = new RegExp('(^(?!RP).+)(\n        )', 'gm')
    
    const str = `RP/HDQ1S/HDQ1SJFDEAK/C8HJ/2024837        20APR23/0339Z   GLS123
            DATA
        001 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E*
        002 OS/AF1439 Q 20APR 4 HAJCDG LK1 0600 0735/LK *1A/E*
        003 OS/AF9466 Q 20APR 4 CDGRNS LK1 1235 1340/LK *1A/E*
        004 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF
            DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133
            0Z
        005 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF
            DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133
            0Z
        006 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF
            DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133
            0Z
        007 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF
            DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133
            0Z
        008 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E*
        009 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF
            DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133
            0Z
        010 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E*
        011 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF
            DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133
            DATA
        012 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E*`;
    
    // The substituted value will be contained in the result variable
    let flag = true;
    let result = str;
    while(flag){
        flag=false;
        result = result.replace(regex, function(match, p1){flag=true;return p1;});
    }
    
    console.log('Substitution result: ', result);
    Login or Signup to reply.
  2. If a positive lookbehind is supported in your environment, you can assert that the previous line does not start with RP and then match a newline followed by 6 or 8 spaces which are not followed by a space:

    (?<=^(?!RP).+)n      (?:  )?(?! )
    

    Regex demo

    You could also check if DATA is not after the match

    n      (?:  )?b(?!DATAb)
    

    Regex demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search