your question isn't in the slightest bit anything to do with jQuery. The language you're working with is JavaScript, jQuery is a framework written in JavaScript to aid DOM manipulation.
–
Andy EJul 13 '10 at 8:54

@Andy E But i am using JQuery which is more telling. There may have been some JQuery function to do this easily for all i know. And by stating JQuery you know i am using JavaScript, but if i tagged it with JavaScript, you don't know if im using JQuery.
–
4imbleJul 13 '10 at 9:15

there is no jQuery function to make it easier. Sorry :-)
–
Andy EJul 13 '10 at 9:19

He also needs to "double escape", by escaping the escape character. For example, wherever you see \. you actually need \\..
–
Andy EJul 13 '10 at 8:51

Putting my original expression into new RegExp(" "); causes it to no longer work. But my new reg exp does now do something (not what i was expecting but another problem i suspect). What is the difference between my first expression and the second one that i need to put into the new RegExp()?
–
4imbleJul 13 '10 at 8:57

1

@Kohan first one is a regex literal - where the regex is enclosed in two slashes like /regex/. You don't have to escape backslashes in a regex literal - but you should escape forwards slashes as it is the delimiter. The second one is regex constructor which takes pattern as a string and hence no delimiter is required - neither should you escape forward slashes. But being a string, you gotta escape the backslashes in it
–
AmarghoshJul 13 '10 at 9:03

Okay, you've got the JavaScript syntax straightened out, now let's talk about regex syntax. The colon (:) has no special meaning, so there's no need to escape it. The dot (.) and question mark (?) normally do have special meanings, but not when they appear in a character class (i.e., inside the square brackets).

The hyphen (-) does have special meaning in a character class: it forms ranges, like [a-z] and [0-9]. If you want to include a literal hyphen in a character class, you either escape it with a backslash or place it at the beginning or end of the list. For example, in [a-zA-Z0-9\.-] the final hyphen matches a hyphen, while the other three are used to form ranges. (The backslash in front of the dot is unnecessary, but it doesn't harm anything.)

Now look at [a-zA-Z0-9%:/-_\?\.'~]. The backslashes in front of ? and . are just clutter, but that foruth hyphen is a real problem. It forms a range starting with / and ending with _; if you look at an ASCII character map, you'll see that it includes the digits 0-9 and uppercase letters A-Z, plus

/, :, ;, <, =, >, ?, @, [, \, ], ^, _

...obviously not what the author intended. There's also a lot of unnecessary grouping and duplicate code in that regex, and do you really need to match IP addresses, too? The moral is: don't trust anything you find on RegExLib.com.