- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
基本上,我前段时间一直在玩 Steam 机器人,当你在数组中说出内容时,它会自动回复,即“hello-triggers”数组,其中包含“hi”、“hello”等内容。我做到了,每当它收到一条消息时,它都会使用 indexOf() 检查匹配情况,一切正常,直到我注意到它会注意到“hiasodkaso”或“hidemyass”作为“hi”触发器。
因此它会匹配包含该单词的任何内容,即使它位于单词的中间。
我该如何让indexOf只注意到它是确切的单词,而不是同一个单词中的其他内容?
我没有我使用的脚本,但我会做一个非常相似的例子:
var hiTriggers = ['hi', 'hello', 'yo'];
// here goes the receiving message function and what not, then:
for(var i = 0; i < hiTriggers.length; i++) {
if(message.indexOf(hiTriggers[i]) >= 0) {
bot.sendMessage(SteamID, randomHelloMsg[Math stuff here blabla]); // randomHelloMsg is already defined
}
}
正则表达式不会用于此目的,对吗?因为它用于表达式或其他什么。 (我的英语不太好,ikr)
提前致谢。如果我对某些事情不够清楚,请告诉我,我会以另一种方式编辑/表述它! :)
最佳答案
您可以扩展原型(prototype):
String.prototype.regexIndexOf = function(regex, startpos) {
var indexOf = this.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}
然后做:
var foo = "hia hi hello";
foo.regexIndexOf(/hi\b/);
或者,如果您不想扩展字符串对象:
foo.substr(i).search(/hi\b/);
这两个示例均取自 Is there a version of JavaScript's String.indexOf() that allows for regular expressions? 的最佳答案
关于javascript - 如何使indexOf仅匹配 'hi'作为匹配而不匹配 'hirandomstuffhere'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24706065/
这个问题已经有答案了: printf anomaly after "fork()" (3 个回答) 已关闭 3 年前。 #include #include Void main() { Printf
我目前正在使用此正则表达式:\"([^"]+)?\"。这与基本的带引号的字符串匹配,例如 "Hi"。我需要: 要匹配:(“内容”) 要不匹配:(div:("content")) 要不匹配以下内容:(d
我要打印 hi GrandFather 但它似乎印喜父亲。而且我不明白如何区分 findSpecial 之间的使用和 findVirtual 我希望有人可以帮助我。谢谢 class GrandFath
for(i=0;i #include int main() { for(int i=0;i<2;i++) { if(fork()==0) { printf("Hi %d %d
这个问题已经有答案了: What is the difference between a function call and function reference? (6 个回答) 已关闭 7 年前。
这个问题已经有答案了: JavaScript: using constructor without operator 'new' (2 个回答) 已关闭 7 年前。 RegExp('hi') 和有什么
hi
)我试图用标签( ** )替换每组通配符( )。 例如,如果我有: var stuff = array( "The color *blue*!!!!", "The color *red
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why is the 'this' keyword required to call an extensio
这个问题在这里已经有了答案: How to set HTML value attribute (with spaces) (6 个答案) 关闭 8 个月前。 我有一个选择控件。我尝试用 jquery
我在玩弄 JavaScript 的 NaN 并在对数字/字符串的结果使用 typeof 运算符时发现了一些奇怪的行为。为什么会这样? 最佳答案 typeof 90/"hi" 被执行为 (typeof
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 1 年前。 Improve
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭去年。 Improve thi
我有一个大的 numpy 数组 k,形状未指定,我想构造一个相同形状的数组 d 当 中的相应条目时为 1.0 k 介于两个常量 lo 和 hi 之间,否则为 0.0。 (由于更大的代码在做什么,我不想
为什么以下内容的计算结果为'hi'? 'hi' || true || 50 我对 javascript 并不是很陌生,但我正在通过阅读一些旧书来重新增强我的知识,而且我一生都不明白为什么它的计算结果为
我在确定一个对象是否为空时遇到问题...这是我的代码: 这个值没有数据 >>> x = Booking.objects.filter(date_select='2011-12-3') >>> prin
您好,我已经接触 Python 几周了,现在开始学习 tkinter。该按钮应包含文本 Say hello,当用户单击该按钮时,底部标签应显示名称并在其前面显示 Hi。但是我无法让标签显示“Hi {n
过去几天我一直在尝试执行我的 Google Charts 网页 ( jsfiddle ),但错误似乎一直在变化,即使我除了刷新网页之外没有做任何不同的事情。我尝试过使用 Chrome 的“清空缓存和硬
vim 8.0的:h :hi-link表示:hi link {from-group} NONE将删除一组的hi-link。如何删除所有hi-link定义? hi clear,syn clear和syn
我需要以大写字母的形式输出Hi,该字母包含在C++中的*框中。 image of the output required到目前为止,我只找到了逐行打印每一行的基本方法,但是有没有更聪明的方法来做到这一
我正在做一些编码 bat 练习,但我不太明白 for 循环内部发生了什么。 public static int countHi(String str){ int count = 0;
我是一名优秀的程序员,十分优秀!