简体   繁体   English

如何以这种方式编写javascript Regex它接受6个字符

[英]How do I write javascript Regex in such way it accepts 6 Characters

Javascript正则表达式验证它包含6个字符,其中一个字符(1)应该是一个数字。

var pwdregex  =/^[A-Za-z1-9]{6}$/;

This will guarantee exactly one number (ie a non-zero decimal digit): 这将保证一个数字(即非零十进制数字):

var pwdregex = /^(?=.{6}$)[A-Za-z]*[1-9][A-Za-z]*$/;

And this will guarantee one or more consecutive number: 这将保证一个或多个连续数字:

var pwdregex = /^(?=.{6}$)[A-Za-z]*[1-9]+[A-Za-z]*$/;

And this will guarantee one or more not-necessarily-consecutive number: 这将保证一个或多个不一定连续的数字:

var pwdregex = /^(?=.{6}$)[A-Za-z]*(?:[1-9][A-Za-z]*)+$/;

All of the above expressions require exactly six characters total. 所有上述表达式总共需要六个字符。 If the requirement is six or more characters, change the {6} to {6,} (or remove the $ from {6}$ ). 如果要求是六个或更多个字符,改变{6}{6,}或删除${6}$ )。

All possible combinations of 1 digit and alphanumeric chars with a length of 6. 所有可能的1位数字和字母数字字符组合,长度为6。

if (subject.match(/^[a-z]{0,5}\d[a-z]{0,5}$/i) && subject.length == 6) {
    // Successful match
} else {
    // Match attempt failed
}

[Edit] Fixed obvious bug... [编辑]修正了明显的bug ...

If you are ok with using a function instead of a regex you can do this: 如果您可以使用函数而不是正则表达式,则可以执行以下操作:

var sixAlphaOneDigit = function(s) {
  return !!((s.length==6) && s.replace(/\d/,'').match(/^[A-Za-z]{5}$/));
};

sixAlphaOneDigit('1ABCDE'); // => true
sixAlphaOneDigit('ABC1DE'); // => true
sixAlphaOneDigit('ABCDE1'); // => true
sixAlphaOneDigit('ABCDE'); // => false
sixAlphaOneDigit('ABCDEF'); // => false
sixAlphaOneDigit('ABCDEFG'); // => false

If you want to do it strictly in a regex then you could build a terribly long pattern which enumerates all possibilities of the position of the digit and the surrounding (or leading, or following) characters. 如果你想在正则表达式中严格执行,那么你可以构建一个非常长的模式,它列举了数字位置和周围(或前导或后续)字符的所有可能性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中匹配的一系列字符上向正则表达式添加反引号? - How do I add a backtick to a regex on a range of characters matching in JavaScript? 如何在javascript中使用正则表达式替换特殊字符? - How do I replace special characters with regex in javascript? 如何在javascript正则表达式中的字符之间匹配字符串 - How do I match a string between characters in javascript regex 如何编写一个接受回调函数的jQuery插件方法? - How do I write a jQuery plugin method that accepts a callback function? 如何编写一个接受回调作为参数的jquery函数 - How do I write a jquery function that accepts a callback as a parameter 我如何编写一个接受 10 个字母的字符串并在 Javascript 中返回相应电话号码字符串的 function? - How do i write a function that accepts a 10-character string of letters and returns a corresponding phone number string in Javascript? 如何在javascript中对一组字符进行正则表达式? (\\ /) - How can I regex a group of characters in javascript? (\/) 如果在Javascript中不写怎么办 - How do I write if ''not'' in Javascript 如何在JavaScript中使用正则表达式捕获两个特定字符之间的文本? - How do I use regex in JavaScript to capture the text between two particular characters? 如何编写从字符串中提取多个字符(从字符串开头)的 JavaScript function? - How do I write a JavaScript function that extracts a number of characters from a string (from the beginning of the string)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM