I have the following task to do: Write a web page with two text areas T1 and T2. The user enters a string into the text area T1 to present a regular expression. Then the user enters some text into the text area T2. Your code should output the text of T2 with highlighted elements corresponding to the matches for the regular expression of T1. Additional requirement for my task is to match the last occurrence only.
This is what I have so far:
<script>
var str = "This is really cooly";
str = str.replace(/\ly(?=[^\ly]*)$/, "BA");
document.write(str + '<br>');
</script>
but this only matches if the last word ends with "ly".
I managed to do this with the form but only for the first occurrence.
<html>
<body>
<p id="demo">Click the button to display the matches.</p>
<form name="My_form" id="My_form">
T1:<input type="text" name="T1" id="T1" size=200>
<br>
T2:<input type="text" name="T2" id="T2" size=200>
</form>
<br>
<button onclick="myFunction()">Try it</button>
<script type="text/javascript">
function myFunction()
{
var regex = document.My_form.T1.value;
var n = regex.match(document.My_form.T2.value);
var result = regex.replace(n, '<b>$&</b>');
document.getElementById('demo').innerHTML = result;
}
</script>
</body>
</html>
I don't know how to convert a user input into e pattern to search into a regular expression (I am not sure if that is even possible. This is burning my brain for more than a week and I have less than 24 hours to complete this.