Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urllib.quote_plus() equivalent in JavaScript

Trying to port a snippet of code from Python:

my_input = "this&is£some text"
encoded_input = urllib.quote_plus(str(my_input))

...to JavaScript:

var my_input = "this&is£some text";
encoded_input = encodeURIComponent(my_input);

The minor difference is that urllib.quote_plus() will convert spaces to + instead of %20 (link). Just wondering if anyone could provide any ideas. Currently going with this...

var my_input = "this&is£some text";
encoded_input = encodeURIComponent(my_input).replace(/%20/g,'+');
like image 571
OmidTahouri Avatar asked May 16 '13 11:05

OmidTahouri


1 Answers

Use urllib.quote() instead of quote_plus() in your Python code. If changing the Python code is not a solution, your choice of replacing %20 with + is the preferred method anyway (reference, second paragraph in Description).

like image 93
adrianp Avatar answered Sep 20 '22 23:09

adrianp