There is no reason to use the RegExp constructor here. Just use RegExp literal:
var urlRegex = /^(?:(?:\.\.\/)|\/)?(?:\w(?:[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]]|(?:%\d\d))*\w?)?(?:\/\w(?:[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]]|(?:%\d\d))*\w?)*(?:\?[^
Inside the RegExp literal, you simply write a regular expression, with the exception of / , which now requires escaping, since / used as a delimiter in the RegExp literal.
In the character class, ^ has a special meaning at the beginning of the character class, - has a special meaning between two characters, and \ has a special meaning, which is to hide other characters (basically ^ , - , [ , ] and \ ), and also indicate abbreviated character classes ( \d , \s , \w , ...). [ , ] are used as delimiters for a character class, so they also have special meaning. (In fact, in JavaScript only ] has special meaning, and you can specify [ without escaping inside the character class). Besides the 5 characters listed above, other characters (if they are not involved in the escape sequence with \ ) do not have much meaning.
You can reduce the number of screens \ with the above information. For ^ , unless it is the only character in a character class, you can remove it from the beginning of the character class. For - you can put it at the end of the character class.
var urlRegex = /^(?:(?:\.\.\/)|\/)?(?:\w(?:[\w`~!$=;+.^()|{}\[\]-]|(?:%\d\d))*\w?)?(?:\/\w(?:[\w`~!$=;+.^()|{}\[\]-]|(?:%\d\d))*\w?)*(?:\?[^
What changed:
[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]] [\w`~!$=;+.^()|{}\[\]-]
source share