Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: How to URL encode strings that contain dashes (-)?

I have a string that represents that name of an entity. The name of this entity has a dash in it (-). How can I encode the dash? I believe it should be encoded to '%2D' but when I try encodeURIComponent() or encodeURI() for that matter, it doesn't encode the dash. I was just wondering if there is an encode function that will encode the dash rather than just doing a .replace

like image 703
connected_user Avatar asked Mar 07 '26 22:03

connected_user


1 Answers

- is a character that appears in the ASCII character set and has no special meaning in URLs. While you can encode it as %2D, doing so is not needed nor is it normal. Encoding it would be like using %61 instead of a.

There is no standard encoding function that will encode a - character. replace is the logical choice if you really, really want to.

like image 95
Quentin Avatar answered Mar 09 '26 11:03

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!