Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JavaScript urlEncode (with RegEx?)

Posted on 2001-08-27
6
Medium Priority
?
12,221 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

916 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