gpt4 book ai didi

javascript - 如何从字符串中删除重复的行?

转载 作者:行者123 更新时间:2023-12-02 22:00:50 25 4
gpt4 key购买 nike

如何用 javascript 编写脚本来检查字符串中的所有行 ("Line1\nLine2\nLine3..."),如果有重复行则只留下一个并忽略 br 标签?

var s = "Hello world\n<BR>\nThis is some text\nThis is some text\n<BR>\nThis is some text"
line1 = "Hello world"
line2 = "<BR>"
line3 = "This is some text"
line4 = "This is some text"
line5 = "<BR>"
line6 = "This is some text"

var result = "Hello world\n<BR>\nThis is some text\n<BR>"
line 1 = "Hello world"
line 2 = "<BR>"
line 3 = "This is some text"
line 4 = "<BR>"

最佳答案

我认为最短的解决方案是这样的。

myStr
.split("\n")
.filter((item, i, allItems) => {
return i === allItems.indexOf(item);
})
.join("\n");

关于javascript - 如何从字符串中删除重复的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16973819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com