0

I have this string:

var str = "Toto PARKER rejected the task.<br><br>Approval comments:<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>";

I would like a function to return "NO!!"

To do this i using:

<p>Click the button to display the array values after the split.</p>

<button onclick="myFunction()">Try it</button>   
<p id="demo"></p>

<script>
function myFunction() {
    var str = "Toto PARKER rejected the task.<br><br>Approval comments:<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>";
    var res = str.match("(<br\s*[\/]?>)[1](.*)(<br\s*[\/]?><br\s*[\/]?>)");
    document.getElementById("demo").innerHTML = res[0];          
}
</script>

I have some difficulties with my regex because the return is:

<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>

So, how to start my regex with the last <br> (to return <br>NO!!<br><br>) and after how to not return <br>?

2
  • 1
    Why don't you split with br tag and remove null data from array and take last using pop method of array. If not removing null values then last but one from the array Commented Jul 12, 2017 at 9:19
  • I assume it always would be br tags, so you can use /<br>(.*?)<br>/g Commented Jul 12, 2017 at 9:25

2 Answers 2

1

Here is a solution without a regex. I splitted your string on <br>, then removed the null values, and display the last element from the result:

function myFunction() {
    var str = "Toto PARKER rejected the task.<br><br>Approval comments:<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>";
    var res = str.split('<br>').filter(n => n);
    document.getElementById("demo").innerHTML = res.pop();
}
<p>Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

Sign up to request clarification or add additional context in comments.

Comments

0

A short form...

console.log(
  str.split('<br>').filter( e => e ).pop()
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.