gpt4 book ai didi

javascript - 在 Vugen TruClient 协议(protocol)中的对象步骤中使用 Javascript Regex 评估 JS

转载 作者:行者123 更新时间:2023-12-02 15:05:18 26 4
gpt4 key购买 nike

在使用 TruClient 协议(protocol) (Firefox) 在 Vugen 中创建脚本时,我有一个 Evaluate JS on object 步骤来查找对象,其中以下object.innerHTML:

Foo Bar<br />BAZ
<br />

我需要从这段文本中提取 BAZ 以便在其他地方使用,所以我有下面是 JS 部分的代码,使用正则表达式提取它:

var regex = /Foo\s+Bar<br\s+\/>(.*)\s*<br \/>/i;  // Shows as red in the TC JS editor, but no error icon shows, so not sure what the error may be.
var matches = [];
var match;
matches = regex.exec(object.innerHTML);
match = matches[matches.length - 1];
window.alert(match); // For debugging purposes

但是,当我运行脚本时,失败并出现以下错误:

** 6: Evaluate JavaScript var regex = /Foo\s+B... alert(match); on Foo Bar
** failed - an argument is invalid: 'Code': JavaScript exception
'TypeError: matches is null' during evaluation

我已经测试了这个正则表达式 here ,并且它有效正如预期的那样。

使用webtoolkit online JS tester ,我已经成功地测试了代码的以下变体,以确保它提取我需要的内容:

var data = "Foo Bar<br />BAZ<br />";
var regex = /Foo\s+Bar<br\s+\/>(.*)\s*<br \/>/i;
var matches = [];
var match;
matches = regex.exec(data);
match = matches[matches.length - 1];
alert(match);

这将按预期返回 BAZ

编辑

我最初认为这是 Vugen/TruClient 特定问题。然而,在阅读了 Michael Galos 的回答( below )之后,我意识到这是一个通用的 Javascript 问题,所以我也添加了 Javascript 标签。

最佳答案

您正在寻找的是正则表达式中的“单行”标志。
不幸的是,Javascript 中不存在(regex101.com 会模拟它)。

要解决这个问题,您可以使用它来绕过换行符:

[\s\S]

因此,在您的情况下,请尝试以下操作:

var regex = /Foo\s+Bar<br\s+\/>(.*)[\s\S]*<br \/>/i; 

关于javascript - 在 Vugen TruClient 协议(protocol)中的对象步骤中使用 Javascript Regex 评估 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158761/

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