0

I have have to convert user input β to "& beta;" (I am leaving space because SO converts to β) while doing a form submit. I will take from the query parameter and send to the API. I looked for many library

This one does not convert. it works for & to & amp; http://amalhashim.wordpress.com/2012/10/19/jquery-html-encode-decode/

and

This one convert to different format, http://www.tumuski.com/code/htmlencode/

But I need & beta; .

I need a common function to htmlencode all special characters not β alone..

Please help..

4
  • Why do you want to convert a character to an HTML entity before putting it into a URL? URLs aren't HTML. Commented Jul 24, 2013 at 9:46
  • I have to get β from Request and send it to the API. The API does not handle β. I have to convert β to &beta ; and send to the API through ajax. Commented Jul 24, 2013 at 9:49
  • boggle, that is one really broken API. Commented Jul 24, 2013 at 9:49
  • Ohh.. thank you.. is there any way I can convert β to & beta; using javascript Commented Jul 24, 2013 at 9:52

1 Answer 1

0

Try this:

var mystring = $('#inputid').val();
mystring = mystring.replace(/β/g,"& beta;");

UPDATE for more characters:

var mystring = $('#inputid').val();
var chars = {}

//ALL THE CHARS YOU WANT TO REPLACE
chars["ß"] = "& beta;"
chars["other"] = "& other;"
//..

for(var chr in chars){
   mystring = mystring.replace(new RegExp(chr, 'g'), chars[chr]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi its not only for beta I need for all special characters.. sorry for not clear in my question, i ill update it..
@Dilip Rajkumar ok, I've updated the answer, let me know if it is useful.
Thanks Ben.. it is not about specific special characters, its about all special characters.. user can input any special character and I should handle it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.