I am having issues with perfecting this smart quote javascript. I have a template using both plain and rich text. I have successfully gotten the smart quotes to show in the correct direction a majority of the time.
One issue is when an apostrophe is used as the first character in the field, both in a plain text and rich text field.
The second issue is when quotes the first character in the rich text editor only.
My code is:
Rich Text Rule
function replaceFunction(field)
{
// replace quotes at the beginning or end of the entire string first
field = field.replace(/^\"(?=[\w|$][^<>]*<)/, "„");
field = field.replace(/^\'(?=[\w|$][^<>]*<)/, "‚");
field = field.replace(/\"$(?=[^<>]*<)/, "”");
field = field.replace(/\'$(?=[^<>]*<)/, "’");
// now replace quotes before or inside words
field = field.replace(/\s"(?=[\w|$][^<>]*<)/g, " „");
field = field.replace(/\s'(?=[\w|$][^<>]*<)/g, " ‚");
// finally, replace all other quotes (after words)
field = field.replace(/\"(?=[^<>]*<)/g, "”");
field = field.replace(/\'(?=[^<>]*<)/g, "’");
return field;
}
if (Field("Body") != "")
return replaceFunction(Feild("Body"))
else
return "";
Plain Text Rule
function replaceFunction(field)
{
// replace quotes at the beginning or end of the entire string first
field = field.replace(/^\"/, "„");
field = field.replace(/^\'/, "‚");
field = field.replace(/\"$/, "”");
field = field.replace(/\'$/, "’");
// now replace quotes before or inside words
field = field.replace(/\s"(?=[\w|$])/g, " „");
field = field.replace(/\s'(?=[\w|$])/g, " ‚");
// finally, replace all other quotes (after words)
field = field.replace(/\"/g, "”");
field = field.replace(/\'/g, "’");
return field;
}
if (Field("Headline") != "")
return replaceFunction(Field("Headline"))
else
return "";
I tested modifying the codes a few ways but don't really know how to right the correct fix. I added a line to the "now replace quotes before or inside words" but wasn't able to get it right exactly.
During testing, I could add the following line to the replace quotes before or inside words to fix the double quotes:
field = field.replace(/\"(?=[\w|$][^<>]*<)/g, "„");
, but I can't do this for the single quote because it changes the direction of an apostrophe (’) to a left single quote.
Can someone help me as I am not very good at creating javascript code?
Thanks!