Solved

JavaScript urlEncode (with RegEx?)

Posted on 2001-08-27
6
12,150 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to hide html tags in text area 4 37
Why does my button disappear? 3 29
Interview question Javascript, database 12 36
how can i count words? 2 21
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

861 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

22 Experts available now in Live!

Get 1:1 Help Now