26ph19
26ph19

Reputation: 393

Remove weird characters using jquery/javascript

I have this string ‘Some string here’. I want to remove these weird characters(‘, ’) from this string. I am currently using replace() function but it does not replace it with empty string. Below is the script. How can I remove it?

  for (var i = 0, len = el.length; i < len; i++) {
      $(el[i]).text().replace("‘", "");
  }

Upvotes: 3

Views: 8589

Answers (5)

Govind Singh
Govind Singh

Reputation: 15490

you have to just remove the elements whose ascii value is less then 127

var input="‘Some string here’.";
var output = "";
    for (var i=0; i<input.length; i++) {
        if (input.charCodeAt(i) <= 127) {
            output += input.charAt(i);
        }
    }
 alert(output);//Some string here.

fiddle link

OR

remove your loop and try

$(el[i]).text().replace("‘","").replace("’","");

Upvotes: 5

manoz
manoz

Reputation: 91

.filter('SpecialCharacterToSingleQuote', function() {
        return function(text) {
            return text ? String(text).replace(/â/g, "'").replace(/&#128;|&#153;|&#156;|&#157;/g, "") : '';
        };
    });

Upvotes: 0

Alex P
Alex P

Reputation: 6072

Those weird characters probably aren't so weird; they're most likely a symptom of a character encoding problem. At a guess, they're smart quotes that aren't showing up correctly.

Rather than try to strip them out of your text, you should update your page so it displays as UTF-8. Add this in your page header:

<meta charset="utf-8" />

So why does this happen? Basically, most character encodings are the same for "simple" text - letters, numbers, some symbols - but have different representations for less common characters (accents, other alphabets, less common symbols, etc). When your browser gets a document without any indication of its character encoding, the browser will make a guess. Sometimes it gets it wrong, and you see weird characters like ‘ instead of what you expected.

Upvotes: 1

V31
V31

Reputation: 7666

Created a fiddle for your problem solution

Code Snippet:

var str = "‘Some string hereâ€";
str = str.replace("‘", "");
str = str.replace("â€", "");
alert(str);

Upvotes: 0

Server Khalilov
Server Khalilov

Reputation: 417

This code works fine for me:

"‘Some string here’".replace("‘","").replace("’","");

Upvotes: 0

Related Questions