BFS algorithm to ccheck captured group, but has typeError indicated in chrome