javascript - "If"与 JavaScript 中的 "and"

标签 javascript jquery regex function

我在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/

相关文章:

javascript - 如何解析插值中的范围名称?

javascript - Socket.IO 服务器性能和带宽使用

javascript - 将类添加到轮播中的事件幻灯片

python - 如何循环通过 div 类以访问其中的 li 类?

javascript - 如何修改我的垂直可折叠内容面板?

javascript - 打开多个盒子以获取 "interactive tutorial"

javascript - 当 ng-show 可以适用于两种情况时,为什么要使用 ng-hide

jquery - 无法让 jQuery datepicker tr 类工作

regex - 为什么这个正则表达式在 grep 中有效但在 sed 中无效?

python - 使用 Python 从 csv 文件读取特定日期范围