Home>

I'd like to implement a string check with a regular expression, but it doesn't work and let me ask you a question.
For examplehogeOrhoge2When I try to write a regular expression that just matches exactly, the following works.

const r ='^ (hoge | hoge2) $'
const rr = new RegExp (r,'g')
console.log ('hoge'.match (rr))
console.log ('hoge2'.match (rr))
console.log ('hoge3'.match (rr))
//->Array ["hoge"]
//->Array ["hoge2"]
//->null


However, this time@hoge,@ hoge2I wrote the following to make it an exact match, but it doesn't work

const r ='@ ^ (hoge | hoge2) $'
const rr = new RegExp (r,'g')
console.log ('@ hoge'.match (rr))
console.log ('@ hoge2'.match (rr))
console.log ('@ hoge3'.match (rr))
//->null
//->null
//->null

If anyone knows how to write it and it will respond correctly, please let me know.