Error replace () Javascript Jquery

1

I'm doing a function in Jquery and Javascript in which I have to pass a parameter through the url.

This parameter contains the characters / and + , so I have to replace them so that there are no problems in the url.

When using the code:

var selectId = $("#parametroSel").val().replace(/\//g, ":::").replace(/+/g, "::::");

In the javascript console, this error is logged:

SyntaxError: nothing to repeat

What could be the problem?

    
asked by Mauricio Arias Olave 29.06.2016 в 12:28
source

2 answers

2

I have already found the problem, it was here:

replace(/+/g, "::::");

I had to escape the +

replace(/\+/g, "::::");
    
answered by 29.06.2016 / 12:39
source
2

I know the question is about replace, but I suggest you use a encodeURIComponent() to encode data that you must send in a query string.

The main thing is that it performs standard encoding or URL encoding, which will be easy, even transparent to decode on the server side.

var uri = "http://ejemplo.com/prueba uno.html?symbols=\+:/&";

console.log(
  encodeURIComponent(uri)
);
    
answered by 29.06.2016 в 12:51