为什么我可以加入所有字符串
我有一个问题,我正在使用JS。假设我的文本由段落分开:
test Hello
test1 Hello
test2 Hello
test3 Hello hello hello hello hello hello hello test
3,然后像这样加入所有段落:test hello hello test1 hello test2 hello test3 hello test3你好,我做了
<pre>
var x = string.trim();
var xArr = x.match(/^\n|\S+/gm);
var xJoin = xArr.join(" ");
</pre>
:段落像以前一样继续,我也尝试使用string.replace()。在X.Match(/^\ n | \ s+/gm)之后,它给我带来了这样的数组:
<pre>
[, ,
Test, hello
, ,
Test1, hello
, ,
Test2, hello
, ,
Test3, hello
, ,
]
</pre>
所以这对我来说似乎有些奇怪,难道是不是真的被线路断裂所隔开吗?有什么想法如何将琴弦彼此相邻?谢谢。
I have a issue, I'm using JS. Let's say I have this text separated by paragraphs:
Test hello
Test1 hello
Test2 hello
Test3 hello
and then I want to join all the paragraphs next to each other like this: Test hello Test1 hello Test2 hello Test3 hello, so I did this:
<pre>
var x = string.trim();
var xArr = x.match(/^\n|\S+/gm);
var xJoin = xArr.join(" ");
</pre>
but it continue as before with paragraphs, I try with string.replace() too. After the x.match(/^\n|\S+/gm) it brings me the array like this:
<pre>
[, ,
Test, hello
, ,
Test1, hello
, ,
Test2, hello
, ,
Test3, hello
, ,
]
</pre>
So this seemed a bit strange to me, could it be that it is not really separated by line breaks? Any idea how to put the strings next to each other? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下
Try this
您需要修复代码中的一些小问题,
^
(字符串的启动)。You need to fix some minor issues in your code
^
( this start of string ) from you pattern您的段落是HTML元素吗?
如果是这样,您可以使用此信息:
或者,在使用上述方法之前,将其放入由JavaScript创建的HTML元素。
否则,您可以尝试此正则表达式?
text.replace(/(/(\ r \ n | \ n | \ r)/gm,“”);
Is your paragraph in an html element?
If so you can just use this:
Or, put it in an html element created from your javascript before using the above method.
Otherwise you can try this regex?
text.replace(/(\r\n|\n|\r)/gm, "");