Replace all backslashes in a string with a pipe

I have the string: \rnosapmdwq\salesforce\R3Q\OutputFiles\Archive

I’m getting a unrecognized escape sequence when I try to send this to a .NET web service.

I’m trying to replace all of the “\” with “|” to send it to the server.

I know I can use the replace method but that only replaces the first element. I think I need to use a regular expression to solve it.

Here’s what I have so far:

Path = Path.replace("\\/g", "|");

This is wrong though.

You don’t need to make a regex a string, and it helps having that first / in there

Path = Path.replace(/\\/g, "|")

The correct syntax would be: Path = Path.replace(/\\/g, "|");

Working example at:

Example (extra code for demonstration purposes only):

var Path = $("#path").text();
Path  = Path.replace(/\\/g, "|");

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   JavaScript - Get minutes between two dates

Similar Posts