This is sophisticated, and I like the idea of supporting Unicode characters outside good ole ASCII; but we have an existing (cruder) function for doing this in WebappOSUtils.jsm (https://github.com/mozilla/mozilla-central/blob/3cfdb57ce92b9305b412b365e0341d6086e5f0a1/toolkit/webapps/WebappOSUtils.jsm#L262-L268), and I suspect we're better off reusing that function (and then making it more liberal, although I'm worried that we were conservative there for good reason).