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

如何用JavaScript正则按指定HTML片段分割Selenium提取的字符串?

解决方案

直接用你指定的完整分隔符进行字符串分割即可,不需要先替换 为空格再处理,这样能精准匹配分割点,不会破坏原字符串里的空格结构。

以Python为例,代码如下:

# 从Selenium获取的innerHTML字符串
target_str = 'Automation  Donot Delete - Automation 1 <i class="arrow"></i> GoogleContacts - Automation'
# 定义目标分隔符
split_delimiter = ' <i class="arrow"></i> '
# 执行分割
result_list = target_str.split(split_delimiter)
print(result_list)

运行后会得到你想要的结果:

['Automation  Donot Delete - Automation 1', 'GoogleContacts - Automation']

为什么之前的方法失效?

你之前先替换 为空格再按多空格分割,会把原字符串中Automation后面的两个连续空格也当成分隔符,导致第一个字段被拆分成了['Automation', 'Donot Delete - Automation 1'],而直接用完整的分隔符分割可以精准定位到箭头元素所在的分割位置,保留原字符串里的空格。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 17:44:56