为了账号安全,请及时绑定邮箱和手机立即绑定

提取字符串的特定部分

提取字符串的特定部分

猛跑小猪 2023-08-24 15:59:13
我有一个下面的字符串,我需要提取字符串的某些部分。 input :  'response:"The check has enabled" '     output:  The check has enabled有没有更好的方法,我已经通过下面的代码片段实现了。let string = 'response:"The check has enabled" 'let output = string.replace(  `response:"`,  "")output = output.substring(0, output.length - 2);console.log(output)
查看完整描述

1 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

您可以使用正则表达式来匹配"s 之间的文本:


const string = 'response:"The check has enabled" '

const output = string.match(/(?<=")[^"]+(?=")/)[0];

console.log(output)


如果您控制创建需要解析的 API string,那么最好对其进行更改,以便它为您提供 JSON,然后您可以使用JSON.parse它,并且只需访问.response对象的属性即可。


查看完整回答
反对 回复 2023-08-24
  • 1 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信