For Example:
"first line<br><br>Second line"
should be replaced with "first line<br>Second line"
"first line<br><br><br>Second line"
should be replaced with "first line<br><br>Second line"
and so on...
What I am trying to do is replace all newline characters in a string recieved from textarea with <br>
in a string using the regex: "str.replace(/(?:\r\n|\r|\n)/g, '<br>')"
but this puts one extra <br>
tag because when someone is typing in the textarea they press enter twice instead of once for a newline.
Code:
<textarea style="width:200px;height:200px" id="first"></textarea>
<textarea style="width:200px;height:200px;" id="second"></textarea>
<button id="submit">submit</button>
<script type="text/javascript">
const firstTextArea = document.querySelector('#first');
const secondTextArea = document.querySelector('#second');
const submitBtn = document.querySelector('#submit')
submitBtn.addEventListener('click' , function(){
let str = firstTextArea.value.replace(/(?:\r\n|\r|\n)/g, '<br>');
secondTextArea.value = str;
})
</script>