soum
soum

Reputation: 1159

replacing special characters with a specific special character

I have a string which is exactly like this...

R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%2fSka,

I have tried enough to remove the special characters before they reach the browser...but not going anywhere..so planing to rely on my old and trusted friend "javascript" I want it to read

R&B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae&Ska,

I know this can be done through regular expression which I am just not able to figure it out. How would I write the expression?

Any help would be highly appreciated

Upvotes: 0

Views: 77

Answers (3)

codebased
codebased

Reputation: 7073

Probably you need decodeURIComponent() function.

<script>
 var decodedString = decodeURIComponent('R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%2fSka');
</script>

http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

Upvotes: 1

Ty H.
Ty H.

Reputation: 953

Look at these answers:

Regex to remove all special characters from string?

They layout a regex that will remove everything EXCEPT those characters you want to allow, this is safer then removing a list of %26,%2f, etc.

For example...

[^0-9a-zA-Z, ]+ would allow all letters, numbers, commas and whitespace.

[^0-9a-zA-Z]+ would be only letters and numbers

The other answers are probably pointing you in a better direction... if it means fixing the string before it gets to the client.

Upvotes: 1

user2575725
user2575725

Reputation:

You may try using:

decodeURIComponent("R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%26Ska")
//^prints^ "R&B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae&Ska"

Upvotes: 2

Related Questions