这是我的函数,它应该改变HTML输入的onClick属性,但是如果我使用的话
document.getElementById('buttonLED'+id).onclick = "writeLED(1,1)";
Run Code Online (Sandbox Code Playgroud)
它根本不起作用,但如果我使用的话
document.getElementById('buttonLED'+id).onclick = writeLED(1,1);
Run Code Online (Sandbox Code Playgroud)
该函数自行执行!在单击按钮之前,我必须使用哪些代码来更改onCLick属性而不执行该函数?
这是完整的功能,如果重要的话:
function showLED(id){
if(color == 0){
document.getElementById('buttonLED'+id).onclick = "writeLED(1,1)";
document.getElementById('buttonLED'+id).value="light is on";
//document.getElementById('buttonLED'+id).disabled = false;
}else{
document.getElementById('buttonLED'+id).onclick = "writeLED(1,0)";
document.getElementById('buttonLED'+id).value="light is off";
//document.getElementById('buttonLED'+id).disabled = false;
}
}
Run Code Online (Sandbox Code Playgroud) 所以我对这个主题做了一些研究,但我找不到完美的解决方案.例如,我在变量中有一个字符串.
var="a1b1c2"
Run Code Online (Sandbox Code Playgroud)
现在我想要做的只是匹配"a"跟随任何数字,但我只希望它返回"a"之后的数字来匹配它的规则如
'a\d'
Run Code Online (Sandbox Code Playgroud)
因为我只需要数字,我试过
'a(\d)'
Run Code Online (Sandbox Code Playgroud)
也许它确实在某个地方捕获了它,但我不知道在哪里,这里的输出仍然是"a1"
我还尝试了一个非捕获组忽略输出中的"a",但在perl正则表达式中没有效果:
'(?:a)\d'
Run Code Online (Sandbox Code Playgroud)
作为参考,这是我的终端中的完整命令:
[root@host ~]# var="a1b1c2"
[root@host ~]# echo $var |grep -oP "a(\d)"
a1 <--output
Run Code Online (Sandbox Code Playgroud)
可能没有-P(一些非perl正则表达式格式)也可能,我很感谢每个答案:)
编辑: 使用
\K
Run Code Online (Sandbox Code Playgroud)
并不是真正的解决方案,因为我不一定需要比赛的最后部分.
EDIT2: 我需要能够获得比赛的任何部分,例如:
[root@host ~]# var="a1b1c2"
[root@host ~]# echo $var |grep -oP "(a)\d"
a1 <--output
but the wanted output in this case would be "a"
Run Code Online (Sandbox Code Playgroud)
编辑3: 使用"后视断言"几乎解决了问题,例如:
(?<=a)\d
Run Code Online (Sandbox Code Playgroud)
不会返回字母"a",只返回它后面的数字,但它需要一个固定的长度,例如它不能用作:
(?<=\w+)\d
Run Code Online (Sandbox Code Playgroud)
EDIT4: 到目前为止最好的方法是使用perl或结合使用后视断言和\ K但它似乎仍然有一些限制.例如:
1234_foo_1234_bar
1234567_foo_123456789_bar
1_foo_12345_bar
if "foo" and "bar" are place-holders for words that don't always have the same length,
there is no …Run Code Online (Sandbox Code Playgroud) 我试图在一个数组中存储一个字符串中的一些变量(数字).就像,如果我会使用类似的东西
"1 0 0 1 1 0 0 1 1"
Run Code Online (Sandbox Code Playgroud)
将每个数字存储在一个数组中,如:
numbers[i];
Run Code Online (Sandbox Code Playgroud)
我相信这有一个PHP函数,感谢帮助.