6

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

Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115
  • 1
    You need to provide a [mcve]: Show how you define `mystring`. Show how you test the value of `result`. – Quentin Dec 17 '16 at 18:49
  • why are you escaping it in a string? – epascarello Dec 17 '16 at 18:49
  • "which is the proper regex" — Why are you asking about regex when you are using a string and not a regex? – Quentin Dec 17 '16 at 18:50
  • 1
    _"it's not working"_ Cannot reproduce – guest271314 Dec 17 '16 at 18:58
  • @Quentin you're right, so how should it be? `res.replace(/\/\//, "/");` ?? – Sredny M Casanova Dec 17 '16 at 19:00
  • @SrednyMCasanova Can you include text of `mystring` and expected result of `result` at Question? – guest271314 Dec 17 '16 at 19:01
  • @SrednyMCasanova — The replace method accepts a string as the first argument. What it "should" be depends on what the problem is. The code does what you say it should do so it isn't clear what the problem is. – Quentin Dec 17 '16 at 19:01
  • 1
    @SrednyMCasanova — Re edit: Don't just *describe* the strings, provide a [mcve] – Quentin Dec 17 '16 at 19:03
  • @Quentin Good point. The `RegExp` at Question should return expected result, unless the `RegExp` is replacing `//` following protocol at URL, instead of at `//someotherparameter`. `mystring = "http://mywebpage/someparameter//someotherparameter"; result= mystring.replace("\/\/", "/");console.log(result)` – guest271314 Dec 17 '16 at 19:04
  • Given the data added to the question: I can't reproduce the problem: http://jsbin.com/jiyohim/1/edit?js,console – Quentin Dec 17 '16 at 19:06
  • @SrednyMCasanova How do you get the original string? – guest271314 Dec 17 '16 at 19:08
  • Related: [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Jonathan Lonowski Dec 18 '16 at 05:38

2 Answers2

22

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

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

console.log(
  'hi// hello//123//'.replace(/\/\//g, '/')
)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
6

There is no need to escape it if it is a string used as a replacement

console.log("asd//qwe".replace("//","/"));

If it were a regular expression, you would need to escape it

console.log("asd//qwe".replace(/\/\//,"/"));

Now if there is more than one set, than you need to use a regular expression with a global modifier.

console.log("asd//qwe".replace(/\/\//g,"/"));
epascarello
  • 204,599
  • 20
  • 195
  • 236