开发者

Regular expression to turn hyphens into double hyphens

My implementation of markdown turns double hyphens into endashes. E.g., a -- b becomes a – b

But 开发者_C百科sometimes users write a - b when they mean a -- b. I'd like a regular expression to fix this.

Obviously body.gsub(/ - /, " -- ") comes to mind, but this messes up markdown's unordered lists – i.e., if a line starts - list item, it will become -- list item. So solution must only swap out hyphens when there is a word character somewhere to their left


You can match a word character to the hyphen's left and use a backreference in the replacement string to put it back:

body.gsub(/(\w) - /, '\1 -- ')


Perhaps, if you want to be a little more accepting ...

gsub(/\b([ \t]+)-(?=[ \t]+)/, '\1--')

\b[ \t] forces a non-whitepace before the whitespace through a word boundary condition. I don't use \s to avoid line-runs. I also only use one capture to preserve the preceding whitespace (does Ruby 1.8.x have a ?<= ?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜