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

Asked

Viewed 2,530 times

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 were tested on the Regexr site as in the example here: http://regexr.com/3eo6t

2 answers

5


In javascript a regex is delimited / doesn’t need simple quotes.

Change:

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

To:

console.log(str.replace(/\//g,'')); 
  • I was already answering my own question with that same rsrs, I traveled here putting as string a regex

  • 1

    @Leandroluk depends on the language, some precise quotation marks and delimiters.

  • that’s why I made a mistake, to learn a language beat in college, I swaddled all kkk

1

One option is to break through / and then put together.

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

  • but it is still not the best logical option. doing this you run 2 methods instead of only 1

  • It is to be a different option than what has already been answered, not the best, for you or want to know that there is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.