English 中文(简体)
Javascript regular expression not work in hexachloro
原标题:Javascript regular expression not working in Firefox
  • 时间:2009-11-04 01:37:52
  •  标签:

这一定期表达方式在“E”组织中失败,但在“E”公司工作。

function validate(x) {
   return /.(jpeg|jpg)$/ig.test(x);
}

谁能解释为什么?

最佳回答

如果你只用档案名称进行测试,那么,确定全球标识的旗帜就具有重大意义,因为你在铺设任何通道的末尾,即:

function validate(x) {
  return /.(jpeg|jpg)$/i.test(x);
}

var imagename =  blah.jpg ;
alert (validate(imagename));    // should be true
imagename =  blah.jpeg ;
alert (validate(imagename));    // should be true
imagename =  blah.png ;
alert (validate(imagename));    // should be false

所有三个测试都是按预期在FF中进行的。

关于它如何发挥作用——经常表达方式可以相当trick,但我将解释上述模式的细节:

  • the slash / is used to delimit the pattern
  • the dot . has a special meaning of any non-whitespace character - writing it as . forces the patten to only match a fullstop
  • the () mean a collection (it gets more complicated, but that covers this usage)
  • the | means or
  • $ means the end of the string (or the end of a line in a multi-line text)
  • the i after the second slash means case insensitive
  • the g means global - which doesn t make much sense in this case, so I removed it.

因此。

/.(jpeg|jpg)$/i

意思是“在......jpeg或.jpg时断断断断续”

因此。. .JPEG, .jpg, .JpeG, etc... will all pass now...

问题回答

“......本身是指“任何性质”。 你们指的是“......”?





相关问题
热门标签