Avatar of hankknight
hankknight
Flag for Canada asked on

JavaScript: URL Encode All Non-whitelisted Characters

I want JavaScript functions that will work just like escape() but I want all non-whitelisted charachters to be encoded.

These charachters should be whitelisted:

bcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_

Because the letter "A" is not whitelisted, it should be URL encoded.

For example:
A test, 123-456

Should become:
%41%20test%2C%20123%2D456%20
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Demo</title>
</head>
<body>

<script type="text/javascript">
//<![CDATA[

var x = 'A test, 123-456';

alert(escapeNonWhitelisted(x));

function escapeNonWhitelisted(str) {
 return escape(str);
}

//]]>
</script>

</body>
</html>

Open in new window

JavaScript

Avatar of undefined
Last Comment
leakim971

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes