如何编写正则表达式在JavaScript中按HTTPS链接及空格拆分字符串
拆分字符串中的独立URL部分
我需要处理包含URL的字符串,例如:
"The site https://example.com contains all the information you need"
最初使用的正则表达式:
const rgx_link = /(?=https?:\/\/)/gi;
拆分后得到的结果是:
arr = ["The site ", "https://example.com contains all the information you need"];
我的需求是将URL单独拆分为一个独立元素,期望结果为:
arr = ["The site ", "https://example.com", " contains all the information you need"];
尝试过((?=https?:\/\/)(\s))但未达到预期效果,经测试,使用正则/(https?:\/\/\S*)/gi可以满足需求。该正则会完整匹配从http://或https://起始,到第一个空白字符结束的URL内容,以此作为拆分依据即可将URL从字符串中独立拆分出来。
内容的提问来源于stack exchange,提问作者WeAreDoomed
相关产品推荐
相关产品推荐

