简体   繁体   中英

Replace a backslash comma to double backslash comma

How do I change replace a "\\," to "\\\\," ? So that, the string OU=b\\,aditi,DC=zeus2,DC=com becomes OU=b\\\\,aditi,DC=zeus2,DC=com ??

I need some javascript solution, regex or replace function will do.

You can use the following:

str = str.replace(/\\,/g, "\\\\,");

See DEMO

 var str = 'OU=b\\\\,aditi,DC=zeus2,DC=com'; alert("before: "+str); str = str.replace(/\\\\,/g, '\\\\\\\\,'); alert("after: "+str); 

(?=\\,)

Try this.Replace by \\ .See demo.

https://regex101.com/r/pG1kU1/25

var re = /(?=\\,)/gm; 
var str = 'OU=b\,aditi,DC=zeus2,DC=com';
var subst = '\\'; 

var result = str.replace(re, subst);

 var re = /\\\\/; var str = 'OU=b\\\\,aditi,DC=zeus2,DC=com'; var subst = '\\\\\\\\'; var result = str.replace(re, subst); document.getElementById("results").innerHTML = result 
 <div id="results"></div> 

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM