Home > Net >  Replace forward slash with backward then forward slash
Replace forward slash with backward then forward slash

Time:11-17

I want to replace all forward slashes with a backward slash followed by a forward slash. Example:

../src/services/resource_mgmt/

Should be converted to

..\/src\/services\/resource_mgmt\/

I don't know hot use replaceAll(...) with the correct regex.

CodePudding user response:

Just in case you also want to achieve that without regex, this should do the trick

var url = "../src/services/resource_mgmt/";

const replaceSlash = (url) => {
var result = [];
var array = url.split("");
array.forEach(el => {
el === "/" ? result.push("\\/") : result.push(el);
})
result = result.join("");
return result;
};
console.log(replaceSlash(url))

CodePudding user response:

const url = '../src/services/resource_mgmt/';
const newUrl = url.replaceAll(/\//g, '\\/');
console.log(newUrl);

  • Related