gpt4 book ai didi

javascript - 如何使用 .replace with match() 方法 javaScript

转载 作者:行者123 更新时间:2023-11-29 16:23:54 26 4
gpt4 key购买 nike

我需要替换一些从 match() 获得的数据;

这一个返回包含“总时间:9 分 24 秒”的字符串

data.match(/Total time: [0-9]* minutes [0-9]* seconds/);

但我只需要“9 分 24 秒”,我尝试使用:

data.match(/Total time: [0-9]* minutes [0-9]* seconds/).replace("Total time:", "");

但是出现错误""

".replace is not a function"

谁能帮帮我?

最佳答案

在正则表达式中使用捕获子表达式:

var match = data.match(/Total time: ([0-9]* minutes [0-9]* seconds)/);
alert(match[1]);

match() 返回一个数组,这就是为什么您不能对结果调用 replace — 没有 Array#replace方法。

关于javascript - 如何使用 .replace with match() 方法 javaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8121543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com