0

我正在尝试检查 src 是否有超过五个连续的零。例如http://domain.com/images/00000007.jpg会匹配,但http://domain.com/images/0000_1.jpg不会。这是我到目前为止所拥有的,但它似乎不起作用。有什么建议么?

if (src.match(/0{5,}$/)) {
  match found
}
else {
  no match found
}
4

3 回答 3

3

您应该从字符串的开头匹配零^,即

if (/^0{5,}/.test(src)) { ... }

如果您需要在字符串的任何位置匹配 5 个连续的零,则省略任何^or $

更新:在您的情况下,您可以使用类似if (/\/0{5,}/.test(src)) { ... }.

于 2013-04-23T23:14:20.163 回答
2

作为替代方案,您也可以使用indexOf(),类似于:

if(src.indexOf('00000') > -1){
    alert('matchFound');
} else {
    alert('no match found');
}
于 2013-04-23T23:16:18.230 回答
0

试穿这个尺寸:

/0{5,}[^\/]*$/

它检查五个或更多零,然后是除正斜杠之外的任何内容,直到字符串的末尾。如果要进行其他验证,可以使用正斜杠开始模式以确保文件以五个零开头,或者您可以在末尾添加可接受的文件类型:

/\/0{5,}[^\/]*\.(jpe?g|gif|png)$/i

细分(对于您或未来读者不知道的任何部分):

/             Starts the regular expression
  \/          A literal forward slash (escaped because '/' is a delimiter)
  0{5,}       Five or more zeros
  [^\/]*      Anything except a literal forward slash, zero or more times.
  \.          A literal period (unescaped periods match anything except newlines)
  (           start a group
    jpe?g     jpeg or jpg (the ? makes the 'e' match zero or 1 times)
    |         OR
    gif       gif
    |         OR
    png       png
  )           End group
  $           Assert the end of the string.
/             End the regular expression
i             Case insensitive.
于 2013-04-23T23:18:37.417 回答