Solved

JavaScript urlEncode (with RegEx?)

Posted on 2001-08-27
6
12,180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…

738 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