   concat: (pattern) ->
     result = (@reduce(part) for part in pattern)
     result = (part for part in result when part)
-    return result.join('').replace(/\s/g, '').trim()
+    return @safechars(result.join('')).trim()
 
   reduce: (step) ->
     value = @methods[step.method].apply(@, step.arguments)

    for pattern in @patterns
      citekey = ''
      for candidate in pattern
        citekey = @clean(@concat(candidate))
        break if citekey != ''
      citekeys.push(citekey)
    return citekeys

class BetterBibTeXPatternFormatter
  constructor: (@patterns) ->

  unsafechars: Zotero.Utilities.XRegExp('[^-\\p{L}0-9_!\$\*\+\.\/;\?\[\]]', 'g')
  punct: Zotero.Utilities.XRegExp('\\p{Pc}|\\p{Pd}|\\p{Pe}|\\p{Pf}|\\p{Pi}|\\p{Po}|\\p{Ps}', 'g')
  caseNotUpperTitle: Zotero.Utilities.XRegExp('[^\\p{Lu}\\p{Lt}]', 'g')
  caseNotUpper: Zotero.Utilities.XRegExp('[^\\p{Lu}]', 'g')

  format: (item) ->
    @item = Zotero.BetterBibTeX.serialized.get(item)
