Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  128] [ 2]  / answers: 1 / hits: 24439  / 11 Years ago, wed, may 15, 2013, 12:00:00

I'm using a regex pattern that I got from regexlib to validate relative urls. On their site you can test the pattern to make sure it fits your needs. Everything works great on their site, as soon as I use the pattern in mine I get the error message:




Invalid range in character class




I know that this error usually means that a hyphen is mistakenly being used to represent a range and is not properly escaped. But in this case since it works on their site I'm confused why it's not working on mine.



var urlRegex = new RegExp('^(?:(?:../)|/)?(?:w(?:[w`~!$=;-+.^()|{}[]]|(?:%dd))*w?)?(?:/w(?:[w`~!$=;-+.^()|{}[]]|(?:%dd))*w?)*(?:?[^#]+)?(?:#[a-z0-9]w*)?$', 'g');


NOTE:
If you're going to test the regex from their site (using the link above) be sure to change the Regex Engine dropdown to Client-side Engine and the Engine dropdown to Javascript.


More From » regex

 Answers
13

Either put - at the end or beginning of the character class or use two backslashes to do a regex escape within string



since you are using string you need to use two backslashes for each special characters..






NOTE



Check out this answer on SO which explains when to use single or double backslashes to escape special characters


[#78218] Tuesday, May 14, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tai

Total Points: 466
Total Questions: 87
Total Answers: 116

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;