Which characters need to be escaped in regex?

2,700 views
Skip to first unread message

tom saffell

unread,
May 14, 2013, 2:51:22 PM5/14/13
to jsh...@googlegroups.com
JSHint gives me this error in my JS:

Unexpected escaped character '<' in regular expression.

So I removed the preceding \, and the error went away - great.

Can someone point me to the definitive list of characters that need to be escaped in JS regex?
There is this question on StackOverflow, but it contradicts the rule of the '<', and seem to be influenced by PHP.

Also, seems the JSHint doesnt mind the '>' being escaped, which makes me want that list even more..

I'm using grunt-contrib-jshint v0.1.1 

thanks

tom


Wolfgang Kluge

unread,
May 14, 2013, 4:14:17 PM5/14/13
to jsh...@googlegroups.com
Hi,
complete list for literals - as far as I know - is

1. Outside [ ]
[ ] { } \ / | . ? + *

2. Inside [ ]
[ ] \ / - ^

it's not always needed to escape "-" inside brackets, but it's safer ( think of e.g. [a-c-e] which is the same as [abc\-e]). ^ has to be escaped only if it's at the beginning of the set.

Greetings, Wolfgang

tom saffell

unread,
May 14, 2013, 4:17:26 PM5/14/13
to jsh...@googlegroups.com
Thanks Wolfgang. I'll use that.

tom
Reply all
Reply to author
Forward
0 new messages