Question: I had a list of different formats for the string. When a string produces a string of something, the users must key-in the format of that particular string.

If the given string are

> 
> A-01-13A
> 
> 
> (One alphabet, follow by dash, two numbers, follow by dash, two numbers, and one alphabet)
> 
> 
> 

or

> 
> A-01-13
> 
> 
> (One alphabet, follow by dash, two numbers, follow by dash, two numbers)
> 
> 
> 

---

The user's key-in string is **validated** below:

> 
> B-13-99
> 
> 
> B-14-11A
> 
> 
> 

The user's key-in string is **NOT validated** below:

> 
> B-13-100
> 
> 
> 

---

What is the best way to validate the given string using Regex or other methods in Javascript?

Answer: 
Just use simple regex:

```
const userKeyRegExp = /^[A-Z]\-[0-9]{2}\-[0-9]{2}[A-Z]?$/;

const valid = userKeyRegExp.test('A-01-13A');

console.log(valid);

```
you can do it in the following way 

```js
function validate(str){
    return str.match(/^[A-Z]-\d{2}-\d{2}[A-Z]?$/) !== null;
}

console.log(validate('A-01-13A'));
console.log(validate('A-01-13'));
console.log(validate('B-13-99'));
console.log(validate('B-14-11A'));
console.log(validate('B-13-100'));
```