You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求符合特定格式要求的HTML input pattern正则表达式规则

Solution for Your HTML Input Pattern Requirement

No problem at all! Let's craft the exact regex pattern you need by mapping your requirements step by step:

Step 1: Define the single valid substring

Each valid substring needs 3-4 lowercase letters followed by exactly one digit. That translates to:

[a-z]{3,4}[0-9]
  • [a-z]{3,4}: Matches 3 to 4 lowercase English letters
  • [0-9]: Matches exactly one digit (0-9)

Step 2: Handle multiple substrings with commas

We need to allow one or more of these substrings, separated by commas, with no leading or trailing commas. Here's how to combine that:

^[a-z]{3,4}[0-9](,[a-z]{3,4}[0-9])*$

Let's break this down:

  • ^: Anchors the match to the start of the input (prevents leading commas or invalid characters before the first substring)
  • [a-z]{3,4}[0-9]: The first valid substring (required, so empty input is rejected)
  • (,[a-z]{3,4}[0-9])*: Matches zero or more additional substrings, each preceded by a comma (the * means this part is optional, so single-substring inputs are allowed)
  • $: Anchors the match to the end of the input (prevents trailing commas or invalid characters after the last substring)

Step 3: Apply it to your input element

Here's the ready-to-use code for your form:

<form>
  <input type="text" name="test" pattern="^[a-z]{3,4}[0-9](,[a-z]{3,4}[0-9])*$" title="Please enter valid substrings (3-4 lowercase letters + 1 digit), separated by commas (no leading/trailing commas)">
</form>

Note: Adding a title attribute is helpful—it shows a custom message to users when their input doesn't match the pattern.

Test Cases

This pattern will match all your valid examples:

  • anke1
  • anke1,our4
  • anke1,our4,tim2
  • anke1,our4,tim2,next4

And reject invalid inputs like:

  • anke (no digit)
  • anke1, (trailing comma)
  • Anke1 (uppercase letter)
  • anke10 (two digits)
  • ,anke1 (leading comma)

内容的提问来源于stack exchange,提问作者Qamelion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:57:48