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
Iām halfway to getting this working and will persevere on my own - you guys get it right round ye.
A regex which pretty much covers any possible valid scenario might look similar to the following one …
… 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 …