如何拆分字符串,以JavaScript中的特定字符断开?
要在每次〜时拆分字符串,请拆分数组。拆分后,为〜的每次出现添加换行符,即<br>。
例如,
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>热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短