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

如何从字符串中提取数组?正则替换方法无效求助

如何从字符串中提取数组?

当前字符串

"['Biller.Customer.Data@Taxonomy', 'Product.Platform and Enterprise Services Data.Data@Taxonomy']"

期望结果

['Biller.Customer.Data@Taxonomy', 'Product.Platform and Enterprise Services Data.Data@Taxonomy']

尝试过的无效方法

string.replace(/"/g, '')

解决方案

你的正则没效果,是因为目标字符串里的是双引号字符,而不是"这个HTML实体字符串。针对不同需求,有几种处理方式:

1. 去除所有双引号

直接匹配双引号字符进行全局替换:

string.replace(/"/g, '')

2. 仅去除首尾的双引号(更安全)

如果担心字符串内部可能存在双引号,只去掉外层的包裹双引号:

string.replace(/^"|"$/g, '')

3. 转换为实际的JS数组

如果最终需要得到可直接使用的数组对象,需先将内部单引号替换为双引号(符合JSON格式要求),再解析:

const resultArray = JSON.parse(string.replace(/'/g, '"').replace(/^"|"$/g, ''))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 21:14:55