This seems brittle to me.  There are libraries for doing UA checks (we have one in butter) you should use.