Checking strings for patterns in JavaScript