支持正则表达式吗?
function extractFirstCode(text) {
if (!text) return null;
const candidates = text.match(/[A-Za-z0-9]{4}/g);
if (!candidates) return null;
for (const s of candidates) {
if (/[0-9]/.test(s) && /[A-Za-z]/.test(s)) {
return s;
}
}
return null;
}
怎么查看脚本异常信息
回答
是的,Hamibot 支持正则表达式。在 Hamibot 脚本中,你可以使用正则表达式来匹配文本内容,例如通过 textMatches
方法来查找符合特定正则表达式的控件。
你提供的函数 extractFirstCode
使用了正则表达式来提取符合条件的字符串,这种用法在 Hamibot 中也是可行的。不过需要注意的是,Hamibot 的 JavaScript 引擎是 Rhino 1.7.15,它对某些 JavaScript 特性可能不完全兼容,因此建议避免使用箭头函数等较新的 JavaScript 特性。
如果你需要在 Hamibot 脚本中实现类似功能,可以参考以下示例:
// 文档地址 https://docs.hamibot.com/
function extractFirstCode(text) {
if (!text) return null;
var candidates = text.match(/[A-Za-z0-9]{4}/g);
if (!candidates) return null;
for (var i = 0; i < candidates.length; i++) {
var s = candidates[i];
if (/\d/.test(s) && /[A-Za-z]/.test(s)) {
return s;
}
}
return null;
}
这个函数在 Hamibot 中应该可以正常运行,但需要注意脚本的兼容性问题。如果遇到问题,可以尝试调整代码以适应 Rhino 引擎。
补充回答 1
在 Hamibot 中,你可以通过以下方式查看脚本的异常信息:
- 使用
log
函数记录日志:在脚本中添加log
函数来记录执行过程中的信息,包括异常信息。例如:
// 文档地址 https://docs.hamibot.com/
try {
// 你的代码逻辑
} catch (e) {
log('发生异常: ' + e.message, true);
}
- 使用
postMessage
发送消息:通过hamibot.postMessage
将异常信息发送到控制台,以便远程查看。例如:
// 文档地址 https://docs.hamibot.com/
try {
// 你的代码逻辑
} catch (e) {
hamibot.postMessage('发生异常: ' + e.message);
}
-
查看本地日志:Hamibot 会将脚本的执行日志保存在手机存储中。你可以通过文件管理器找到日志文件,通常位于
/Hamibot/logs/
目录下。 -
使用
exit
方法:在脚本中调用hamibot.exit()
可以结束脚本运行,并在发送消息后停止执行。
通过以上方法,你可以有效地查看和处理脚本中的异常信息。