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

求仅使用Javascript正则表达式将URL转换为指定纯字母域名的方案

实现代码

// 可直接复用的处理逻辑
const getPureDomain = (url) => {
  return url.replace(/^https?:\/\/hello\.|\..*$/g, '').replace(/-/g, '');
}

// 测试示例
console.log(getPureDomain("https://hello.romeo-juliet.fr")); // 输出 romeojuliet
console.log(getPureDomain("https://hello.romeojuliet.co.uk")); // 输出 romeojuliet
console.log(getPureDomain("https://hello.romeo-jul-iet.fr")); // 输出 romeojuliet
console.log(getPureDomain("https://hello.romeo-juliet.com")); // 输出 romeojuliet

正则逻辑说明

  • 第一层replace(/^https?:\/\/hello\.|\..*$/g, ''):
    • 匹配开头的http://hello./https://hello.前缀直接移除
    • 匹配首个点号之后到URL结尾的所有内容(自动适配fr/com/co.uk等任意后缀)直接移除
  • 第二层replace(/-/g, ''):匹配所有横杠字符直接移除,最终得到纯字母的域名主体

内容的提问来源于stack exchange,提问作者zigaish

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 08:27:05