我在java脚本函数中使用if条件,虽然我已经检查过并且&&在java脚本中使用但不知何故它不起作用。任何人都可以建议,这里可能出了什么问题:
if(slugs[i].match("^{{") && slugs[i].match("}}$"))
{
alert(slugs[i] + "YES!");
}
嵌套时检查是否正常工作。
if(slugs[i].match("^{{"))
{
if(slugs[i].match("}}$"))
{
alert(slugs[i] + "YES!");
}
}
最佳答案
简而言之:您应该使用像 slugs[i].match(/^\{\{.*\}\}$/) 这样的检查
另一方面this demo表明一切都按预期进行。问题可能出在其他地方
var slugs = ['{{slug}}'];
var i = 0;
// your example #1
if(slugs[i].match("^\{\{") && slugs[i].match("\}\}$"))
{
alert(slugs[i] + "YES!");
}
// your example #2
if(slugs[i].match("^\{\{"))
{
if(slugs[i].match("\}\}$"))
{
alert(slugs[i] + "YES!");
}
}
// corrected to use a single regex to accomplish the same thing
if(slugs[i].match(/^\{\{.*\}\}$/))
{
alert(slugs[i] + "YES!");
}
关于javascript - "If"与 JavaScript 中的 "and",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17483360/