Solved

JavaScript urlEncode (with RegEx?)

Posted on 2001-08-27
6
12,147 Views
Last Modified: 2007-11-27
Hi,

I need a urlencode function in javascript. I couldnt find one. Is it really true that there is none???

I tried following, but doesnt work. Might be because I dont know how to program RegEx).

    function urlencode(text) {

      text.replace(///,"%2F");
      text.replace(/?/,"%3F");
      text.replace(/=/,"%3D");
      text.replace(/&/,"%26");
      return text;
    }

(I only need those 4 characters encoded)
whats wrong with it?
0
Comment
Question by:exorcist
  • 3
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
bebonham earned 50 total points
ID: 6430133
1. your function (this is a joke so...)

function urlencode(string)
{
return escape(string)
}

2. the answer, of course is, yes, there is a function, it is called escape and unescape.


Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6430145
if you want to see what was wrong with your  regexp


function urlencode(text) {

     text= text.replace(/\//g,"%2F");
    text= text.replace(/\?/g,"%3F");
   text=  text.replace(/=/g,"%3D");
  text=   text.replace(/&/g,"%26");
     return text;
   }


the above will work.

problems w/ yours:

1. wern't assigning result back to text
2. weren't escaping special characters
3. you need to use the g modifier incase you have a string with 2 of any of the above.

Good luck

Bob
0
 
LVL 3

Author Comment

by:exorcist
ID: 6430149
fast question,
fast answer,
fast points.

that's the way I like it.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Author Comment

by:exorcist
ID: 6430152
cool. just read your second comment.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6430161
>that's the way I like it.

me too.

thanks,

Bob
0
 

Expert Comment

by:itsikb
ID: 8015704
Using of escape not good for all the cases


try this sign  +


and look what u get.



0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now