需求:编写首尾必须带斜杠的大小写字母及空格验证正则表达式
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
相关产品推荐
相关产品推荐

