如何在字符串中包含“↵”时打破新行?
我从php服务器得到了这样的字符串var text = "aaaaaaa↵bbbbbb↵cccccc"
,我想输出这个字符串到屏幕上。如何在字符串中包含“↵”时打破新行?
当出现“↵”时,下面的文本将转到新行。我正在使用AngularJS。
如何通过使用纯Javascript或通过AngularJS来实现此目的?
尝试使用替换功能。这是纯粹的JavaScript。
text.replace(/\u21B5/g,'<br/>')
21B5
是↵
应该不是
? –
@TommyLee是的,我修好了。我没有看到OP想要打印为html。 – levi
感谢您的回答,它几乎可行,但我发现如果我用
替换数据,文本确实会在控制台日志中断开该行,但是当我document.write(text)时屏幕上没有任何事情发生,我不知道为什么? –
unicode的,您可以轻松做到这一点做一个正则表达式:
var text = "aaaaaaa↵bbbbbb↵cccccc";
text.replace(/↵/, '<br/>');
this will only replace the first one, by adding the g (global) parameter we will replace any occurence of this symbol, we simply put the g after the/like so
text.replace(/↵/g, '<br/>');
基本上在这里我们将数据存储在一个名为文本然后我们使用变量字符串/正则表达式方法调用替换它.replace()它需要两个参数:要搜索的模式和我们将要替换的模式;
这只会取代第一次出现。 – levi
是的,我的坏,你必须添加代表全球的g参数,并将搜索任何发生,然后将其替换:text.replace(/↵/ g,'
'); –
MaieonBrix
var newString = mystring.replace(/↵/g, "<br/>");
alert(newString);
你可以在THIS找到更多的链接。在问你之前,你应该真的谷歌它。它在这里SO
使用str.split([separator[, limit]])
:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
var text = "aaaaaaa↵bbbbbb↵cccccc";
for(var i=0;i<text.split('↵').length;i++){
// use text[i] to print the text the way you want
// separated by <br>, create a new div element, whatever you want
console.log(text[i]);
}
改变所有↵到''
也许? –
'str = str.replace(\ u21B5/g,'
');' –
@GeorgeStocker你在'\ u21B5/g'开始时错过'/' – levi