Aneesh
Aneesh

Reputation: 1203

backward slash to forward slash using javascript

I want to replace "\" with "/" in a javascript string.

var p = "D:\upload\date\csv\sample.csv";

to:

var p = "D:/upload/date/csv/sample.csv";

But I am getting error in first line itself. "SyntaxError: malformed Unicode character escape sequence".

How to do this ? Please help. Thanks.

Upvotes: 3

Views: 7676

Answers (3)

neu-rah
neu-rah

Reputation: 1693

also

p=p.split("\\").join("/");

Upvotes: 0

Yoshi
Yoshi

Reputation: 54659

The first one should be var p = "D:\\upload\\date\\csv\\sample.csv";

A single \ is for escaping (or other stuff). In your case the \upload is a problem because \u would indicate an unicode character.

To replace, use: p = p.replace(/\\/g, '/');

Upvotes: 3

clyfish
clyfish

Reputation: 10470

var p = 'D:\\upload\\date\\csv\\sample.csv';
p = p.replace(/\\/g, '/');

Upvotes: 0

Related Questions