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

需求:编写首尾必须带斜杠的大小写字母及空格验证正则表达式

Regex for Validating Strings with Leading/Trailing Slashes and Letters/Spaces

Got it, here's the regex solution tailored exactly to your requirements, plus a breakdown to help you understand how it works:

The Regular Expression

^\/[A-Za-z ]+\/$

Breakdown of Each Component

  • ^: Ensures the match starts right at the beginning of the string (no extra characters before the opening slash)
  • \/: Escapes the forward slash (since it's a special character in regex syntax) to explicitly match the opening /
  • [A-Za-z ]+: Matches one or more of the allowed characters: uppercase letters (A-Z), lowercase letters (a-z), and regular spaces (the space inside the character set)
  • \/: Matches the closing forward slash
  • $: Ensures the match ends right at the end of the string (no extra characters after the closing slash)

Test Cases to Verify

Valid Matches

  • /John Doe/ (your example)
  • /Jane Smith/
  • /ALICE/
  • / Bob / (leading/trailing spaces inside the slashes are allowed)

Invalid Matches

  • John Doe/ (missing opening slash)
  • /John Doe (missing closing slash)
  • /John!Doe/ (contains an exclamation mark, which isn't permitted)
  • // (no content between slashes — see note below if you want to allow this)

Optional Adjustment

If you need to allow empty content between the slashes (like //), just swap the + (one or more) with * (zero or more):

^\/[A-Za-z ]*\/$

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:47:12