拆分字符串,其中包含一个字符到数组项

tct*_*c91 0 javascript arrays jquery

我在句子中有几个字符(〜).在每个〜,我想在此之前将文本拆分为数组项.然后每个数组项都需要一个<br />标记.

我尝试使用替换,但它只适用于第一个〜而忽略其余部分.这就是为什么我认为拆分成阵列可能更有利.

$("h2").html(function(index, currentHtml) {
    return currentHtml.replaceAll('~', '<br />');
});
Run Code Online (Sandbox Code Playgroud)

例:

这是一些文本〜这是文本下面的一些文本〜这里有一些文字〜还有最后一点文字

<strong>This is some text</strong><br />
This is some text underneath that text<br />
some more text here<br />
<strong>and a final bit of text</strong>
Run Code Online (Sandbox Code Playgroud)

ᾠῗᵲ*_*ᵲᄐᶌ 6

您可以使用split with join

$("h2").html(function(index, currentHtml) {
    return currentHtml.split('~').join('<br />');
});
Run Code Online (Sandbox Code Playgroud)

或将其更改为正则表达式

$("h2").html(function(index, currentHtml) {
    return currentHtml.replace(/~/g,'<br/>'); // g signifying global
});
Run Code Online (Sandbox Code Playgroud)