How to remove all occurrences of "/" from a string using Javascript

4

Hello, I'm having trouble applying a simple regex with Javascript. So:

var str = "/Date(1421287200000-0200)/";
console.log(str.replace('/\//g',''));             //não funciona, mesmo estando certo
console.log(str.replace('/[/]/g',''));            //não funciona, mesmo envolvendo "/"
console.log(str.replace('/','').replace('/','')); //funciona, mas não tem lógica

How to solve this problem only with regex in Javascript?

NOTE : The first two examples have been tested on the Regexr site as in the example here: link

    
asked by anonymous 24.11.2016 / 18:07

2 answers

4

In javascript a regex is delimited / does not need single quotation marks.

Change:

console.log(str.replace('/\//g','')); 

To:

console.log(str.replace(/\//g,'')); 
    
24.11.2016 / 18:11
1

One option is to break by / and then merge.

var str = "/Date(1421287200000-0200)/";
str = str.split("/").join("");
console.log(str);
    
24.11.2016 / 18:08