This question already has an answer here:
There is not a simple answer to this question at this site yet. This question aims to give a simple and clear answer.
It is usually recommended to use a variable (
$regex) in this construct:
if [[ $var =~ $regex ]]; then …
Why is that so?