Skip to main content
edited title
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Regex for curly quotes and apostrophes (CoffeScript/Javascript)

edited tags
Link
200_success
  • 144.1k
  • 22
  • 188
  • 473
deleted 7 characters in body
Source Link
wyc
  • 555
  • 1
  • 3
  • 9

So, afterAfter years of fear and procrastination I decided to learn regular expressions. This is the result:

CoffeScript:

str = "<h2><a name=\"#{chapter._id}\">#{chapter.title}</a></h2>\n\n#{chapter.content}\n\n"

str.replace(/>([^>]+)</g, (r) ->
  r.replace(/(>|\s)"/g, "$1“")
   .replace(/"/g, "”")
   .replace(/("|\s)'/g, "$1‘")
   .replace(/'/g, "’")
)

JavaScript:

var str = "<h2><a name=\"" + chapter._id + "\">" + chapter.title + "</a></h2>\n\n" + chapter.content + "\n\n";

str.replace(/>([^>]+)</g, function(r) {
  return r.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:

<p class="">"Yes," he said, "I met her. She's very 'friendly.'"</p>

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. (So I can improve my abilities in this mystical art called regular expressions.)

So, after years of fear and procrastination I decided to learn regular expressions. This is the result:

CoffeScript:

str = "<h2><a name=\"#{chapter._id}\">#{chapter.title}</a></h2>\n\n#{chapter.content}\n\n"

str.replace(/>([^>]+)</g, (r) ->
  r.replace(/(>|\s)"/g, "$1“")
   .replace(/"/g, "”")
   .replace(/("|\s)'/g, "$1‘")
   .replace(/'/g, "’")
)

JavaScript:

var str = "<h2><a name=\"" + chapter._id + "\">" + chapter.title + "</a></h2>\n\n" + chapter.content + "\n\n";

str.replace(/>([^>]+)</g, function(r) {
  return r.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:

<p class="">"Yes," he said, "I met her. She's very 'friendly.'"</p>

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. (So I can improve my abilities in this mystical art called regular expressions.)

After years of fear and procrastination I decided to learn regular expressions. This is the result:

CoffeScript:

str = "<h2><a name=\"#{chapter._id}\">#{chapter.title}</a></h2>\n\n#{chapter.content}\n\n"

str.replace(/>([^>]+)</g, (r) ->
  r.replace(/(>|\s)"/g, "$1“")
   .replace(/"/g, "”")
   .replace(/("|\s)'/g, "$1‘")
   .replace(/'/g, "’")
)

JavaScript:

var str = "<h2><a name=\"" + chapter._id + "\">" + chapter.title + "</a></h2>\n\n" + chapter.content + "\n\n";

str.replace(/>([^>]+)</g, function(r) {
  return r.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:

<p class="">"Yes," he said, "I met her. She's very 'friendly.'"</p>

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.

Source Link
wyc
  • 555
  • 1
  • 3
  • 9
Loading