测试用例(正确时返回空值,错误时返回分类:Bilibili视频ID错误):
id=114514
→id=1145141919810
→1145141919810(长度不对)id=0114514
→0114514(起始位为0)id=av114514
→id=AV114514
→id=av1145141919810
→av1145141919810(纯数字部分长度不对)id=av0114514
→av0114514(纯数字部分起始位为0)id=bV1XX4X1X7XX
→id=bV1ff14stxdy
→bV1ff14stxdy(不符合倒数第三位为7、第五位为1、第七位为4的要求)id=bV1Il1O0fbqc
→bV1Il1O0fbqc(含有小写字母l、大写字母I、大写字母O和数字0)id=bV1z?wsmphxg
→bV1z?wsmphxg(含有非大小写字母和数字的字符)id=bV1xxapi
→bV1xxapi(长度不对)id=1XX4X1X7XX
→id=1ff14stxdy
→1ff14stxdy(不符合倒数第三位为7、第五位为1、第七位为4的要求)id=1Il1O0fbqc
→1Il1O0fbqc(含有小写字母l、大写字母I、大写字母O和数字0)id=1z?wsmphxg
→1z?wsmphxg(含有非大小写字母和数字的字符)id=1xxapi
→1xxapi(长度不对)id=XX4X1X7XX
→id=1X4X1X7XX
→(正确,避免出现1开头的9位id被错认为带有前缀1的10位格式的错误)id=ff14stxdy
→ff14stxdy(不符合倒数第三位为7、第五位为1、第七位为4的要求)id=Il1O0fbqc
→Il1O0fbqc(含有小写字母l、大写字母I、大写字母O和数字0)id=z?wsmphxg
→z?wsmphxg(含有非大小写字母和数字的字符)id=xxapi
→xxapi(长度不对)return { validation = function(frame) local result = true local id = mw.text.trim(frame.args.id or '') if mw.ustring.match(id, '^%d+$') then -- 纯数字 if not (string.sub(id, 1, 1) ~= '0' and string.len(id) <= 9) then -- 若id为纯数字时,当首位不为0且长度在1-9位时合法。 result = false end elseif mw.ustring.match(id, '^[aA][vV]%d+$') then -- 若id为【av+纯数字】(不区分大小写)时,当且仅当纯数字部分首位不为0且长度在1-9位时合法,其余情况添加分类 if not (string.sub(id, 3, 3) ~= '0' and string.len(id) <= 11) then result = false end elseif mw.ustring.match(id, '4.1.7..$') then if string.len(id) == 12 then -- 若id为【BV1】开头时,当且仅当id长度为12位,且除去【BV1】后的部分均不包含小写l大写I、大写O和数字0时合法。 if not mw.ustring.match(id, '^[bB][vV]1[a-km-zA-HJ-NP-Z0-9]+$') then result = false end elseif string.len(id) == 10 then -- 若id为【1】开头时,当且仅当id长度为10位,且除去【1】后的部分均不包含小写l大写I、大写O和数字0时合法。 if not mw.ustring.match(id, '^1[a-km-zA-HJ-NP-Z0-9]+$') then result = false end elseif string.len(id) == 9 then -- 若id不满足上述四种情况时,当且仅当id长度为9位,且不包含小写l大写I、大写O和数字0时合法。 if not mw.ustring.match(id, '^[a-km-zA-HJ-NP-Z0-9]+$') then result = false end else result = false end else result = false end if result then return '' else return frame:expandTemplate {title = 'ArticleCategory', args = {'Bilibili视频ID错误'}} .. '<span class="BilibiliVideoIDValidatorArgument" style="display: none;">' .. frame.args.id .. '</span>' end end }