slinky2000
slinky2000

Reputation: 2673

Searching for backslash + string in javascript via regex

I've got a file reference in JS and I need to parse it via regex. All as I want is to get the 'C' character that follows a backslash. Does anyone know why this doesn't work?

var str = "C:\Course\folder\file.txt";
str.match(/\\C/g);

If I run this in firebug or similar tool, I get nothing back.

Upvotes: 0

Views: 96

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074266

Does anyone know why this doesn't work?

Because the string you've quoted doesn't contain backslashes. It has an invalid escape sequence (\C) resulting in just C and two formfeeds (\f), but no backslashes.

If you have actual backslashes, it works:

var str = "C:\\Course\\folder\\file.txt";
str.match(/\\C/g);

Upvotes: 4

Related Questions