replace ’ with javascript (not ' )
Asked Answered
V

3

8

i'm having a problem replacing ==> (apostrophe) with a space i know it looks so easy but what i mean is editors don't type apostrophe like this ==> but like this ==> ' and i can't find a way to replace it using

var newtext = old.replace(/'/g,"");

here's an example http://jsfiddle.net/zYK9f/4/ in this example you can type ==> in the page but not in the code editor tried a lot but no result hope you can help thanks sorry i mean apostrophe not semicolon

Villager answered 13/2, 2012 at 19:50 Comment(10)
That looks like an apostrophe to me...Sweven
What kind of "editors" are you referring to and why is that a problem?Edelstein
I think I've encountered something similar in the past...he may be referring to text that's copied from Microsoft Word that contains either an apostrophe or a quote. MS Word annoyingly replaces these symbols with something fancier, which may not be interpreted well when pasted into other text editors.Bayern
Just FYI (and WolframAlpha backs me up on this): the former character is a right-single-quote, and the latter is an apostrophe. Different things.Punner
FYI: The "other apostrophe" is a "Right Single Quotation Mark": fileformat.info/info/unicode/char/2019/index.htmAnaphora
@danyim: They're both just characters, if your "editor" can't take anything outside 7-bit ASCII, it might be time to get a new one.Edelstein
@MattiVirkkunen: Sure, but does your keyboard have both characters? Mine certainly doesn't.Hyo
i'm using dreamweaver thanks all i used /\u2019/ instead of /'/ and now it's working thanks allVillager
@MattiVirkkunen Well, try writing a line of code that involves apostrophes or quotes in Word, paste that into your normal code editor, and then try to compile it. If you didn't know about this issue beforehand, you'd be hard-pressed to figure out what exactly is throwing the wrench into your failed build...Bayern
@danyim: Why would anybody in their right mind write code into Microsoft Word?Edelstein
S
5

I'm not sure what you are asking for... like the other answers suggested, you can use

var newtext = old.replace(/'|’/g," ");

However, if the character isn't allowed in your editor, you can use the unicode equivalent:

var newtext = old.replace(/\u2019/g," ");
Saxena answered 13/2, 2012 at 20:2 Comment(2)
+1 for mentioning Unicode literals. However, the OP wants to substitute quotation marks with a space, not an empty string; also, the second alternative you propose contains a syntax error (missing a comma between the two arguments of replace) and is not equivalent to the first (missing ' in the regular expression).Engrave
i'm using dreamweaver thanks all i used /\u2019/ instead of /'/ and now it's working thanks allVillager
E
7

Just copy and paste the character to account for both:

var newtext = old.replace(/'|’/g,"");
Enid answered 13/2, 2012 at 19:55 Comment(0)
S
5

I'm not sure what you are asking for... like the other answers suggested, you can use

var newtext = old.replace(/'|’/g," ");

However, if the character isn't allowed in your editor, you can use the unicode equivalent:

var newtext = old.replace(/\u2019/g," ");
Saxena answered 13/2, 2012 at 20:2 Comment(2)
+1 for mentioning Unicode literals. However, the OP wants to substitute quotation marks with a space, not an empty string; also, the second alternative you propose contains a syntax error (missing a comma between the two arguments of replace) and is not equivalent to the first (missing ' in the regular expression).Engrave
i'm using dreamweaver thanks all i used /\u2019/ instead of /'/ and now it's working thanks allVillager
D
1
var old = "you’ll keep’’’ finding more and ''''more ways to use it.";
var newtext = old.replace(/’/g,"");
var newtext = newtext.replace(/'/g,"");
$("#text").html(newtext);

Will get rid of both types of apostrophes. Is this what you want?

Dustin answered 13/2, 2012 at 19:57 Comment(2)
the problem is dreamweaver can't deal with ’ just ' i don't know if it's blindVillager
try using "’". That's the HTML code for the curly apostrophe.Dustin

© 2022 - 2024 — McMap. All rights reserved.