如何将字符串拆分为纯文本,链接和下一行符号?

时间:2018-06-11 18:30:51

标签: javascript regex

有一个字符串:

"I have a link\n for you https://google.com.\n\nAlso http://some.net.\n\n\nSome text"

我有regExp将字符串拆分为链接和纯文本

const text = 'I have a link\n for you https://google.com.\n\nAlso http://some.net.\n\n\nSome text';   
const regExp = /((?:http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(?:\/\S*)?)/g;
const textParts = text.split(regExp);
console.log(textParts);

结果我有:

["I have a link↵ for you ", "https://google.com", ".↵↵Also ", "http://some.net", ".↵↵↵Some text"]

我应该如何修改regExp以便按下一行符号分割?

结果我需要这个

["I have a link", "↵", "for you ", "https://google.com", ".", "↵", "↵", "Also ", "http://some.net", ".", "↵", "↵", "↵", "Some text"]

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法对其进行过滤:

const text = 'I have a link\n for you https://google.com.\n\nAlso http://some.net.\n\n\nSome text';   
const regExp = /((?:http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3})|(\n)/g;
const textParts = text.split(regExp).filter(Boolean);
console.log(textParts);

我使用OR更新了正则表达式的结尾,以检查一个或多个换行符。这导致了一些undefined结果,但这些结果会通过下一行filter调用过滤掉。

如果没有filter,您会看到:

["I have a link", undefined, "↵", " for you ", "https://google.com", undefined, ".", undefined, "↵", "", undefined, "↵", "Also ", "http://some.net", undefined, ".", undefined, "↵", "", undefined, "↵", "", undefined, "↵", "Some text"]

使用过滤器可以看到预期的输出:

["I have a link", "↵", " for you ", "https://google.com", ".", "↵", "↵", "Also ", "http://some.net", ".", "↵", "↵", "↵", "Some text"]