mightymax
mightymax

Reputation: 431

Javascript loop inside matched string in variable

I have a problem that is probably simple to fix, but escapes me completely. Your help is really appreciated.

I have a document which I search for a string then find matches within the string , put those matches into variables for use later. My problem comes in when there are more than one matches inside the string. I can't figure out how you can loop within a variable (found string). Right now it only replaces the first occurrence. I know I need a loop I just don't know how to do it inside a variable.

Thank you for your help, Max

Test data:

<warning>
<dmCode assyCode="00" disassyCode="01" disassyCodeVariant="AAA" infoCode="00W" infoCodeVariant="A" itemLocationCode="A" modelIdentCode="GAASIB0" subSubSystemCode="0" subSystemCode="0" systemCode="00" systemDiffCode="00"/>
<dmCode assyCode="00" disassyCode="02" disassyCodeVariant="AAA" infoCode="00W" infoCodeVariant="A" itemLocationCode="A" modelIdentCode="GAASIB0" subSubSystemCode="0" subSystemCode="0" systemCode="00" systemDiffCode="00"/>
<dmCode assyCode="00" disassyCode="03" disassyCodeVariant="AAA" infoCode="00W" infoCodeVariant="A" itemLocationCode="A" modelIdentCode="GAASIB0" subSubSystemCode="0" subSystemCode="0" systemCode="00" systemDiffCode="00"/>
</warning>

Code:

DMCString = warninglinebreaks.match(/<dmCode.+?>/i);
if (DMCString != null) {
  DMC = DMCString[0];

  match = DMC.match(/modelIdentCode="(.*?)"/im);
  if (match !== null) {
    var modelIdentCode = match[1];
  } else {
    var modelIdentCode = "";
  }

  match = DMC.match(/systemDiffCode="(.*?)"/im);
  if (match !== null) {
    var systemDiffCode = match[1];
  } else {
    var systemDiffCode = "";
  }

  var Regex = /systemCode="(.*?)"/;
  var match = Regex.exec(DMC);
  if (match != null) {
    systemCode = match[1];

  } else {
    systemCode = "";
  }

  match = DMC.match(/\ssubSystemCode="(.*?)"/im);
  if (match !== null) {
    var subSystemCode = match[1];
  } else {
    var subSystemCode = "";
  }

  match = DMC.match(/subSubSystemCode="(.*?)"/im);
  if (match !== null) {
    var subSubSystemCode = match[1];
  } else {
    var subSubSystemCode = "";
  }

  match = DMC.match(/assyCode="(.*?)"/im);
  if (match !== null) {
    var assyCode = match[1];
  } else {
    var assyCode = "";
  }

  match = DMC.match(/disassyCode="(.*?)"/im);
  if (match !== null) {
    var disassyCode = match[1];
  } else {
    var disassyCode = "";
  }

  match = DMC.match(/disassyCodeVariant="(.*?)"/im);
  if (match !== null) {
    var disassyCodeVariant = match[1];
  } else {
    var disassyCodeVariant = "";
  }

  match = DMC.match(/infoCode="(.*?)"/im);
  if (match !== null) {
    var infoCode = match[1];
  } else {
    var infoCode = "";
  }
  match = DMC.match(/infoCodeVariant="(.*?)"/im);
  if (match !== null) {
    var infoCodeVariant = match[1];
  } else {
    var infoCodeVariant = "";
  }

  match = DMC.match(/itemLocationCode="(.*?)"/im);
  if (match !== null) {
    var itemLocationCode = match[1];
  } else {
    var itemLocationCode = "";
  }

  var sFileName = "DMC-" + modelIdentCode +"-"+ systemDiffCode +"-"+ systemCode + "-" + subSystemCode + subSubSystemCode + "-" + assyCode +"-"+ disassyCode + disassyCodeVariant +"-" + infoCode +infoCodeVariant +"-" +itemLocationCode;
}
DMCClean = Wlinebreaks.replace(DMCString, sFileName);

Upvotes: 1

Views: 56

Answers (1)

MarcoS
MarcoS

Reputation: 17711

Instead of

DMCString = warninglinebreaks.match(/<dmCode.+?>/i);
if (DMCString != null) {
  DMC = DMCString[0];

i.e. always using only the first match, you should do a for loop, for example:

DMCString = warninglinebreaks.match(/<dmCode.+?>/ig);
if (DMCString != null) {
  for (var i = 0; i < DMCString.length; i++) {
    DMC = DMCString[i];

    ...

  }
}

this way you loop through the array of your matched lines (DMCString). Note also the added g flag for the regular expression, to match all the lines, and not only the first one, as suggested by @PierreC. comment.

let warninglinebreaks = `
<warning>
<dmCode assyCode="00" disassyCode="01" disassyCodeVariant="AAA" infoCode="00W" infoCodeVariant="A" itemLocationCode="A" modelIdentCode="GAASIB0" subSubSystemCode="0" subSystemCode="0" systemCode="00" systemDiffCode="00"/>
<dmCode assyCode="00" disassyCode="02" disassyCodeVariant="AAA" infoCode="00W" infoCodeVariant="A" itemLocationCode="A" modelIdentCode="GAASIB0" subSubSystemCode="0" subSystemCode="0" systemCode="00" systemDiffCode="00"/>
<dmCode assyCode="00" disassyCode="03" disassyCodeVariant="AAA" infoCode="00W" infoCodeVariant="A" itemLocationCode="A" modelIdentCode="GAASIB0" subSubSystemCode="0" subSystemCode="0" systemCode="00" systemDiffCode="00"/>
</warning>
`;

DMCString = warninglinebreaks.match(/<dmCode.+?>/ig);
if (DMCString != null) {
  for (var i = 0; i < DMCString.length; i++) {
    DMC = DMCString[i];

    console.log('DMC:', DMC);

  }
}

Upvotes: 1

Related Questions