skip to Main Content

I searched around and cannot find anything that could help me. I was hoping for some help.

The javascript code searches a body of text and finds a phrase then it should grab whole text after it finds it.

Works great on single line strings but not on multiple lines šŸ™

Ideally I could get all colours listed in the same text box seperated by commas.

Like give me everything after "Colours affected" until we get to an empty line.
Is this possible?

https://jsfiddle.net/fenster89411/knrzpvta/3/ working example

function extractInfo() {
  var text = document.getElementById('textarea').value
  text = text.split('n')

  for (let i = 0; i < text.length; i += 1) {
    if (text[i].includes('Scheduled Maintenance')) {
      var approverName = text[i].replace('Scheduled', '').trim();
      document.getElementById('approvername').value = approverName;
    }
    if (text[i].includes('Maintenance ID')) {
      var approverEmail = text[i].replace(/.*Maintenance IDD*(d*).*/, '$1').trim();
      document.getElementById('approveremail').value = approverEmail;
    }
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Information Extraction</title>
</head>
<body>

<textarea id="textarea" rows="20" cols="50">
Scheduled Maintenance 44445555666 - Multiple COLOURS Affected
Maintenance ID 444555666 
Colours affected

Yellow
Blue
Black
red
purple
</textarea>

<button onclick="extractInfo()">Extract Information</button>

<!-- Display extracted information -->
<div>
  <label for="approvername">Scheduled Maintenance Name:</label>
  <input type="text" id="approvername" readonly>
</div>
<div>
  <label for="approveremail">Maintenance ID Email:</label>
  <input type="text" id="approveremail" readonly>
</div>


<script src="script.js"></script>

</body>
</html>

2

Answers


  1. Chosen as BEST ANSWER

    Iā€™m halfway to getting this working and will persevere on my own - you guys get it right round ye.


  2. A regex which pretty much covers any possible valid scenario might look similar to the following one …

    const regXColors = /Colourss+affected(?:n+(?:(?:(.*?)(?:n{2,}|n$))|(.*)))/gs;
    

    … and its pattern gets explained at its playground page.

    In order to extract the intended result one would utilize RegExp.prototype.exec and its result array where one can access the values of the pattern’s two capturing groups. Making partially use of the OP’s original example code, an implementation could be achieved like follows …

    function getAllAffectedColors(value) {
      const regXColors =
        // see ... [https://regex101.com/r/dKw9gG/1]
        /Colourss+affected(?:n+(?:(?:(.*?)(?:n{2,}|n$))|(.*)))/gs;
    
      const regXResult = regXColors.exec(value) ?? [];
      const colorValue = (regXResult[1] || regXResult[2]) ?? null;
    
      return (colorValue === null) ? [] : colorValue.split(/n/);
    }
    function logAllAffectedColors() {
      const textValue = document.querySelector('#textarea').value;
      const colorList = getAllAffectedColors(textValue);
    
      console.clear();
      console.log({ colorList, colorString: colorList.join(', ') });
    }
    document
      .querySelector('button')
      .addEventListener('click', logAllAffectedColors);
    body { margin: 0; }
    button { display: block; }
    textarea { width: 40%; }
    .as-console-wrapper { left: auto!important; width: 57%; min-height: 100%; }
    <textarea id="textarea" rows="11" cols="30">
    Maintenance ID 444555666 
    Colours affected
    
    
    Yellow
    Blue
    Black
    red
    purple
    
    </textarea>
    
    <button>log all affected colors</button>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search