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

如何使用正则移除字符串指定内容 提取docPreview参数中的URL

正则提取docPreview包裹的URL方案

核心正则逻辑

要匹配docPreview('开头、')结尾的结构,直接捕获中间的URL内容,正则写法为:
^docPreview\('([^']+)'\)$
其中第一个捕获组([^']+)就是提取出来的目标URL,规则说明:

  • ^ 匹配字符串起始位置
  • docPreview\(' 匹配固定前缀,其中特殊符号(和'需要加转义符\
  • ([^']+) 匹配所有非单引号的连续字符,存入捕获组1
  • '\)$ 匹配固定后缀,到字符串结束位置

常见使用场景

单条字符串提取

  • JavaScript 示例:
const rawStr = "docPreview('https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5')";
const matchRes = rawStr.match(/^docPreview\('([^']+)'\)$/);
const targetUrl = matchRes ? matchRes[1] : '';
  • Python 示例:
import re
raw_str = "docPreview('https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5')"
match_res = re.match(r"^docPreview\('([^']+)'\)$", raw_str)
target_url = match_res.group(1) if match_res else ''

批量替换

如果是在编辑器中批量处理大量同类字符串,查找规则填上述正则,替换规则填$1(部分编辑器需用\1),即可一键把所有docPreview('xxx')格式的内容替换为纯URL。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 23:24:01