หากต้องการแยกสตริงในทุกๆ การเกิด ~ ให้แยกอาร์เรย์ หลังจากแยกแล้ว ให้เพิ่มการขึ้นบรรทัดใหม่ เช่น
สำหรับแต่ละ ~.
ตัวอย่างเช่น
This is demo text 1!~This is demo text 2!~~This is demo text 3!
หลังจากแยกและเพิ่มตัวแบ่งบรรทัดดังต่อไปนี้สำหรับ ~ การเกิดขึ้น −
This is demo text 1! This is demo text 2! This is demo text 3!
ตัวอย่าง
มาดูตัวอย่างฉบับสมบูรณ์กัน
การสาธิตสด
<!DOCTYPE html>
<html>
<body>
<h2>Adding line break</h2>
<script>
var myArray = 'This is demo text 1!~This is demo text 2!~
~This is demo text 3!~This is demo text 4!~
~This is demo text 5!';
document.write("Original Array: " +myArray);
var brk = myArray.split('~');
var res = brk.join(" <br> ");
document.write("<br><br>"+res);
</script>
<p>Each occurrence of ~ adds a line break above.</p>
</body>
</html>