正则表达式生成密钥模式,如下所示

时间:2021-04-19 20:15:06

I want to validate for some activation Key like 5char-5char-5char-5char

我想验证一些激活密钥,如5char-5char-5char-5char

I show above pattern need to check at 6, 12, 18 position has Dash(-) and have total length is 23. I'm new Here please any one help me to Generate RegEx in Javascript.

我显示上面的模式需要检查6,12,18位置有Dash( - )并且总长度是23.我是新的在这里请任何人帮我在Javascript中生成RegEx。

/[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}/ Is it Okay Right Choice ?

/ [A-ZA-Z0-9] {5} - [A-ZA-Z0-9] {5} - [A-ZA-Z0-9] {5} - [A-ZA-Z0-9] { 5} /这是正确的选择吗?

2 个解决方案

#1


0  

Your regex works well by you can make it more elegant like following:

你的正则表达式运作良好,你可以使它更优雅如下:

/^(?:[A-Za-z\d]{5}-){3}[A-Za-z\d]{5}$/

#2


0  

What about this.

那这个呢。

Demo

var data = "5char-5char-5char-5char";
if(data.match(/^(?:[a-z\d]{5}-){3}[a-z\d]{5}$/,"i"))
{
    console.log("Ok");
}

Don't forget to add ^ and $.

别忘了添加^和$。

#1


0  

Your regex works well by you can make it more elegant like following:

你的正则表达式运作良好,你可以使它更优雅如下:

/^(?:[A-Za-z\d]{5}-){3}[A-Za-z\d]{5}$/

#2


0  

What about this.

那这个呢。

Demo

var data = "5char-5char-5char-5char";
if(data.match(/^(?:[a-z\d]{5}-){3}[a-z\d]{5}$/,"i"))
{
    console.log("Ok");
}

Don't forget to add ^ and $.

别忘了添加^和$。