为什么我可以加入所有字符串

发布于 2025-02-13 09:58:07 字数 645 浏览 2 评论 0原文

我有一个问题,我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韶华倾负 2025-02-20 09:58:07

尝试一下

const text = `Test hello

Test1 hello

Test2 hello

Test3 hello`;

console.log(text.split('\n').filter(t => !!t).join(" "));

Try this

const text = `Test hello

Test1 hello

Test2 hello

Test3 hello`;

console.log(text.split('\n').filter(t => !!t).join(" "));

何以心动 2025-02-20 09:58:07

您需要修复代码中的一些小问题,

  1. 如果您想用空间替换新行,
  2. 需要使用^(字符串的启动)。
let str = `Test hello

Test1 hello

Test2 hello

Test3 hello`


var x = str.trim();
var xArr = x.replace(/\n|\r|\r\n/gm, ' ');

console.log(xArr)

You need to fix some minor issues in your code

  1. Need to use replace if you want to replace new line with space
  2. Need to remove ^ ( this start of string ) from you pattern

let str = `Test hello

Test1 hello

Test2 hello

Test3 hello`


var x = str.trim();
var xArr = x.replace(/\n|\r|\r\n/gm, ' ');

console.log(xArr)

一世旳自豪 2025-02-20 09:58:07

您的段落是HTML元素吗?
如果是这样,您可以使用此信息:

const textElement = document.getElementById("text");
console.log(textElement.innerText);
<div id="text">
  Test hello

  Test1 hello

  Test2 hello

  Test3 hello
</div>

或者,在使用上述方法之前,将其放入由JavaScript创建的HTML元素。

否则,您可以尝试此正则表达式? text.replace(/(/(\ r \ n | \ n | \ r)/gm,“”);

Is your paragraph in an html element?
If so you can just use this:

const textElement = document.getElementById("text");
console.log(textElement.innerText);
<div id="text">
  Test hello

  Test1 hello

  Test2 hello

  Test3 hello
</div>

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, "");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文