You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写正则表达式在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 07:42:36