patternjavascriptMinor
Regex for curly quotes and apostrophes
Viewed 0 times
apostrophesquotesforandregexcurly
Problem
After years of fear and procrastination I decided to learn regular expressions. This is the result:
CoffeScript:
JavaScript:
As you can see, the code turns all "straight" quotation characters (plus apostrophe) to “curly” ones. It was a bit hard because the input had stuff like this:
So I had to make sure the quotes inside the HTML tags were not being included. And make the code know that quotations wouldn't always be preceded by a space (sometimes by an
I welcome any suggestion to make the code shorter, more efficient/readable.
CoffeScript:
str = "#{chapter.title}\n\n#{chapter.content}\n\n"
str.replace(/>([^>]+)
r.replace(/(>|\s)"/g, "$1“")
.replace(/"/g, "”")
.replace(/("|\s)'/g, "$1‘")
.replace(/'/g, "’")
)JavaScript:
var str = "" + chapter.title + "\n\n" + chapter.content + "\n\n";
str.replace(/>([^>]+)|\s)"/g, "$1“")
.replace(/"/g, "”")
.replace(/("|\s)'/g, "$1‘")
.replace(/'/g, "’");
});As you can see, the code turns all "straight" quotation characters (plus apostrophe) to “curly” ones. It was a bit hard because the input had stuff like this:
"Yes," he said, "I met her. She's very 'friendly.'"So I had to make sure the quotes inside the HTML tags were not being included. And make the code know that quotations wouldn't always be preceded by a space (sometimes by an
>).I welcome any suggestion to make the code shorter, more efficient/readable.
Solution
Since you know that the double quotes will always be matched, you might try this instead of your first two replaces:
I think that's a little stronger. The unmatched single quotes are harder, and I think what you have is reasonable. I might try this tack though if JavaScript has the \w for word letters. (If not, use [a-zA-Z].)
r.replace(/"([^"]*)"/g, "“$1”")I think that's a little stronger. The unmatched single quotes are harder, and I think what you have is reasonable. I might try this tack though if JavaScript has the \w for word letters. (If not, use [a-zA-Z].)
.replace(/(\w)'(\w)/g, "$1’$2")
.replace(/'([^']*)'/g, "‘$1’")Code Snippets
r.replace(/"([^"]*)"/g, "“$1”").replace(/(\w)'(\w)/g, "$1’$2")
.replace(/'([^']*)'/g, "‘$1’")Context
StackExchange Code Review Q#75699, answer score: 3
Revisions (0)
No revisions yet.