Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaScript urlEncode (with RegEx?)

Posted on 2001-08-27
6
Medium Priority
?
12,207 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 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

704 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