~가 나타날 때마다 배열 값에 줄 바꿈을 추가하려면 먼저 배열을 분할합니다. 분할 후 줄 바꿈(예:~이 나타날 때마다
)을 추가합니다.
예:
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 occurence of ~ adds a line break above.</p>
</body>
</html>