&引用;如果;加上;及;在javascript中
我在java脚本函数中使用了if条件,虽然我已经检查并在java脚本中使用了&&条件,但不知何故它不起作用。有谁能建议,这里可能有什么问题:&引用;如果;加上;及;在javascript中,javascript,jquery,regex,function,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("}}$")) {
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(/^\{\{.*\}}$/)的检查
另一方面,这表明一切都按预期进行。问题可能在其他地方
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!");
}
简而言之:您应该使用类似于
slugs[i].match(/^\{\{.*\}}$/)的检查
另一方面,这表明一切都按预期进行。问题可能在其他地方
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!");
}
虽然引号在某些情况下起作用,但您没有正确地生成regexp 试试这个
if(slugs[i].match(/^{{/) && slugs[i].match(/}}$/))
{
alert(slugs[i] + "YES!");
}
虽然引号在某些情况下起作用,但您没有正确地生成regexp 试试这个
if(slugs[i].match(/^{{/) && slugs[i].match(/}}$/))
{
alert(slugs[i] + "YES!");
}
match
将返回null
如果找不到模式,请尝试以下操作:
if (slugs[i].match("^{{") !== null && slugs[i].match("}}$") !== null)
{
alert(slugs[i] + "YES!");
}
match
将返回null
如果找不到模式,请尝试以下操作:
if (slugs[i].match("^{{") !== null && slugs[i].match("}}$") !== null)
{
alert(slugs[i] + "YES!");
}
如果目标只是确定是否存在匹配,那么最好使用
.test()
而不是.match()
.match
将返回一个数组,或null
,而.test()
将返回一个布尔值。这需要不同的语法:
试试这个:
if (/^{{/.test(slugs[i]) && /}}$/.test(slugs[i])) {
{
alert(slugs[i] + "YES!");
}
如果目标只是确定是否存在匹配,那么最好使用
.test()
而不是.match()
.match
将返回一个数组,或null
,而.test()
将返回一个布尔值。这需要不同的语法:
试试这个:
if (/^{{/.test(slugs[i]) && /}}$/.test(slugs[i])) {
{
alert(slugs[i] + "YES!");
}
您可以合并2个条件
if(x = slugs[i].match(/^{{(.+)}}$/)){
alert(x[1]+"YES!");
}
您可以合并2个条件
if(x = slugs[i].match(/^{{(.+)}}$/)){
alert(x[1]+"YES!");
}
嵌套的
if
s和&
之间没有区别,您的错误肯定在别处
无论如何,我建议使用而不是每次转换为正则表达式的字符串,并调用布尔值而不是进行匹配:
if (/^\{\{/.test(slugs[i]) && /\}\}$/.test(slugs[i]))
alert(slugs[i]+" YES!");
嵌套的
if
s和&
之间没有区别,您的错误肯定在别处
无论如何,我建议使用而不是每次转换为正则表达式的字符串,并调用布尔值而不是进行匹配:
if (/^\{\{/.test(slugs[i]) && /\}\}$/.test(slugs[i]))
alert(slugs[i]+" YES!");
JavaScript中的正则表达式是语法为
/Regex/
的对象,而不是字符串;如果(a.match(“^{”)&&a.match(“}}}$”){console.log(“1”)}它可以工作,因为它将尝试执行新的RegExp(“^{”)
,但是{
必须进行双转义或使用文本regex/^\{\{/
JavaScript,不是java脚本。@米希克:浏览器也很聪明,但从技术上讲应该转义括号。JavaScript中的正则表达式是语法为/Regex/
,而不是字符串的对象。对我来说似乎行得通:a=“{{a}}”;if(a.match(“^{”)和&a.match(“}$”){console.log(“1”);
,因为它会尝试执行新的正则表达式(“^{{')
,但是{
必须进行双转义或使用文本正则表达式/^\{\{/
JavaScript,不是java脚本。@米希克:浏览器也很聪明,但从技术上讲,括号应该转义。不,测试画眉是否正确-不匹配时返回null
。获取其length
属性将破坏脚本。不,测试画眉是否正确-不匹配时返回null
。获取其length
属性将破坏脚本。但请不要这样。您的属性与“{{}}”
不匹配,捕获组是不必要的,并且您忘记了转义大括号。@Bergi我知道他想提醒双大括号内的某些内容。但请不要这样。您的属性与“{}}”不匹配
,捕获组是不必要的,你忘了逃出大括号。@Bergi我知道他想提醒双大括号内的某些东西。基本上使用test
是正确的方法,但这不起作用。@Bergi-谢谢。我有点仓促。更正了。基本上使用test
是正确的方法,但是这不管用。@Bergi-谢谢。我有点匆忙。更正。