Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace double slash in javascript

I'm working in a javascript function, in a given string I need to replace // for only one slash / by now I have:

result= mystring.replace("\/\/", "/");

bt it's not working, I still get the string with double slash, so which is the proper regex to indicate the double slash to the replace function?

I already tried:

  • !//!
  • ////
  • ///g///g

Edit: I'm using it to correct a URL that is saved in the string, for example, sometimes that URL can be something like: mywebpage/someparameter//someotherparameter and that double slash gives problem, so I need to replace it to one single slash like: mywebpage/someparameter/someotherparameter

like image 207
Sredny M Casanova Avatar asked Nov 29 '22 22:11

Sredny M Casanova


1 Answers

Use regex /\/\//(or /\/{2}/) with a global modifier to replace all occurrence.

result= mystring.replace(/\/\//g, "/");

console.log(
  'hi// hello//123//'.replace(/\/\//g, '/')
)
like image 111
Pranav C Balan Avatar answered Dec 01 '22 13:12

Pranav C Balan