Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式:如何匹配整个单词,其中只有一个破折号,但不超过一个破折号?_Regex - Fatal编程技术网

Regex 正则表达式:如何匹配整个单词,其中只有一个破折号,但不超过一个破折号?

Regex 正则表达式:如何匹配整个单词,其中只有一个破折号,但不超过一个破折号?,regex,Regex,我有这些话 verification-asdflkaasidmf verification-sadfim-aisdmfim 我想匹配第一个验证asdflkaasidmf,但不匹配第二个(有两个破折号)另一种方法是: ^[^-]*\-[^-]*$ 其中包括以下内容: ^ begining of the string [^-]* all characters except - zero or more times \- literal - [^-]* all charact

我有这些话

verification-asdflkaasidmf
verification-sadfim-aisdmfim

我想匹配第一个
验证asdflkaasidmf
,但不匹配第二个(有两个破折号)

另一种方法是:

^[^-]*\-[^-]*$
其中包括以下内容:

^      begining of the string
[^-]*  all characters except - zero or more times
\-     literal -
[^-]*  all characters except - zero or more times
$      end of the string

您可以使用此正则表达式:

/^\w+-\w+$/
此正则表达式在开头匹配一个单词,后面是连字符,最后是另一个单词

let arr=['verification-asdflkaasidmf',
“验证sadfim aisdmfim”
];
让正则表达式=/^\w+-\w+$/;

对于(i=0;iYou可以使用;
/^\w+-\w+$/
(验证-.\n)@anubhava,它工作得很好,谢谢!我觉得它也很可读。如果你写一个答案,我可以标记它是否完整,因为你是第一个回答的人?