Instead of using substr/indexOf, you can use one regex to do it all: /(?:^|&)parameterName=([^&]+)/ will match parameterName anywhere in the query string and give you the value as the first group of the match.