我需要字符串的正则表达式

发布于 2024-12-13 03:14:45 字数 1437 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

走过海棠暮 2024-12-20 03:14:45

这可行:

/^[a-z][a-z0-9_]{0,7}$/i

例如,

/^[a-z][a-z0-9_]{0,7}$/i.test('a1234567'); // true
/^[a-z][a-z0-9_]{0,7}$/i.test('01234567'); // false

This would work:

/^[a-z][a-z0-9_]{0,7}$/i

For example,

/^[a-z][a-z0-9_]{0,7}$/i.test('a1234567'); // true
/^[a-z][a-z0-9_]{0,7}$/i.test('01234567'); // false
一身骄傲 2024-12-20 03:14:45

\w 简写适用于所有字母、数字和下划线。 [A-Za-z] 太过分了,/i 标志将获取所有字母,不区分大小写。

因此,满足您需求的超级简单正则表达式是:

/^[az]\w{0,7}$/i

/^[a-z]\w{0,7}$/i.test("a1234567");
> true
/^[a-z]\w{0,7}$/i.test("a12345697");
> false
/^[a-z]\w{0,7}$/i.test("01234567");
> false

The \w shorthand is for all letters, numbers and underscores. [A-Za-z] is overkill, the /i flag will get you all letters, case insensitive.

Therefore, a super simple regex for what you need is:

/^[a-z]\w{0,7}$/i

/^[a-z]\w{0,7}$/i.test("a1234567");
> true
/^[a-z]\w{0,7}$/i.test("a12345697");
> false
/^[a-z]\w{0,7}$/i.test("01234567");
> false
海风掠过北极光 2024-12-20 03:14:45

试试这个:

/^[A-Za-z]{1}[a-zA-Z0-9_]{0,7}$/

Try this out:

/^[A-Za-z]{1}[a-zA-Z0-9_]{0,7}$/
王权女流氓 2024-12-20 03:14:45

试试这个:

/^[a-zA-Z][0-9a-zA-Z_]{0,7}$/

这需要一个字母起始字符,并且可以选择允许最多 7 个字符,其中可以是字母数字或下划线。

编辑:谢谢杰西的更正。

Try this one:

/^[a-zA-Z][0-9a-zA-Z_]{0,7}$/

This requires an alpha start character, and optionally allows up to 7 more characters which are either alphanumeric or underscore.

EDIT: Thanks, Jesse for the correction.

墨落成白 2024-12-20 03:14:45

还有另一个带有前瞻的版本:)

if (subject.match(/^(?=[a-z]\w{0,7}$)/i)) {
    // Successful match
}

解释:

"^" +           // Assert position at the beginning of the string
"(?=" +         // Assert that the regex below can be matched, starting at this position (positive lookahead)
   "[a-z]" +       // Match a single character in the range between “a” and “z”
   "\\w" +          // Match a single character that is a “word character” (letters, digits, etc.)
      "{0,7}" +       // Between zero and 7 times, as many times as possible, giving back as needed (greedy)
   "$" +           // Assert position at the end of the string (or before the line break at the end of the string, if any)
")"  

And another version with lookaheads :)

if (subject.match(/^(?=[a-z]\w{0,7}$)/i)) {
    // Successful match
}

Explanation :

"^" +           // Assert position at the beginning of the string
"(?=" +         // Assert that the regex below can be matched, starting at this position (positive lookahead)
   "[a-z]" +       // Match a single character in the range between “a” and “z”
   "\\w" +          // Match a single character that is a “word character” (letters, digits, etc.)
      "{0,7}" +       // Between zero and 7 times, as many times as possible, giving back as needed (greedy)
   "$" +           // Assert position at the end of the string (or before the line break at the end of the string, if any)
")"  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文