0

I am making a chrome extension that should replace certain words with others. The problem is it won't replace words that contain serbian latin characters like Č, Ć, Ž, Đ or Š. Here is my code:

var exprs = [
  //Example
  [/\bČ\b/g, "Ć"],
]

function replaceTextInNode(node) {
  var value = node.nodeValue
  var expr
  for (expr of exprs) {
    value = value.replace(expr[0], expr[1])
  }
  node.nodeValue = value
}

function walk(node) {
  var child, next

  switch (node.nodeType) {
    case 1:   // element
    case 9:   // document
    case 11:  // fragment
      child = node.firstChild
      while (child) {
        next = child.nextSibling
        walk(child)
        child = next
      }
      break

    case 3:   // text
      replaceTextInNode(node)
      break
  }
}

document.addEventListener("DOMContentLoaded", function(e) {
  walk(document.body)
})

document.addEventListener("DOMNodeInserted", function(e) {
  walk(e.target)
})

Everything is saved as UTF8

Code is based on: Javascript Regex to replace text NOT in html attributes

Thanks everyone in advance.

Community
  • 1
  • 1
  • 1
    Looks like a bug in Javascript handling of `\b` in regex because it works for `/Č/g`. Rework your regex. See also [Javascript + Unicode regexes](http://stackoverflow.com/a/280762) – wOxxOm Jul 19 '16 at 16:07
  • And to add to wOxxOm's remark, `/u` option for regexes is already supported in Chrome. You should try it. – Xan Jul 19 '16 at 18:31

1 Answers1

0

Try using unicode escape characters, for example Č as \u010c. Usually when I have weird character issues converting them to unicode solves it.

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55