#!/bin/bash
haystack="foo 1,bar"
needle='(,|^)foo(,|$)'
if [[ "$haystack" =~ $needle ]]; then
    echo 'exists'
else
    echo 'does not exist'
fi