[英]Regex to match pattern domain\username
I'm trying to write a regex for the below pattern, but I'm doing in an incorrect way.我正在尝试为以下模式编写正则表达式,但我的做法不正确。 Can you please help me?
你能帮我么? Basically the pattern is like domain\username.
基本上该模式就像域\用户名。 Domain should accept alphanumeric plus - as special character, username should only have alphanumeric characters.
域应接受字母数字加 - 作为特殊字符,用户名只能包含字母数字字符。 There should be \ after domain pattern.
域模式后应该有\。 The regex should be in JavaScript, using String.match() method.
正则表达式应该在 JavaScript 中,使用 String.match() 方法。
Example:例子:
Desktop-123\john123
Desktop123\john1
Desktop123\john
Desktop-123\john
Regex:正则表达式:
/^[a-zA-Z0-9-]+\[a-zA-Z0-9]$/
If the domain name can not consist of only -
, you can first match 1+ alpha numberic chars and optionally repeat a -
followed by 1+ alpha numeric chars.如果域名不能只包含
-
,您可以首先匹配 1+ 个字母数字字符,并可选择重复一个-
后跟 1+ 个字母数字字符。
Note to repeat the character class 1 or more times or else you would match a single char.请注意重复字符 class 1 次或更多次,否则您将匹配单个字符。
^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\\[a-zA-Z0-9]+$
^
Start of string ^
字符串开头[a-zA-Z0-9]+
Match 1+ times any of the listed in the character class [a-zA-Z0-9]+
匹配字符 class 中列出的任意项的 1+ 次(?:-[a-zA-Z0-9]+)*
Optionally repeat a -
and any of the listed in the character class (?:-[a-zA-Z0-9]+)*
可选择重复一个-
和字符 class 中列出的任何一个\\[a-zA-Z0-9]+
Match a \
and again match 1+ times any of the listed in the character class \\[a-zA-Z0-9]+
匹配一个\
并再次匹配字符 class 中列出的任何一个 + 次$
End of string $
字符串结尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.