最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Escape special characters in a String - Stack Overflow

programmeradmin5浏览0评论

I have a String: c:\build\target.

This es from an external source, passed directly as it is. I cannot modify it before using it in a javascript.

However \b and \t are special characters, but I'd simply like to print it (or work with it) as it looks like, not as it is interpreted c:uild arget.

How is it possible?

I have a String: c:\build\target.

This es from an external source, passed directly as it is. I cannot modify it before using it in a javascript.

However \b and \t are special characters, but I'd simply like to print it (or work with it) as it looks like, not as it is interpreted c:uild arget.

How is it possible?

Share Improve this question edited Oct 26, 2012 at 14:57 Ash Burlaczenko 25.5k16 gold badges69 silver badges101 bronze badges asked Oct 26, 2012 at 14:54 Balázs NémethBalázs Németh 6,6679 gold badges47 silver badges65 bronze badges 6
  • parse through the string and add another '\' whenever you need to – Gene Parmesan Commented Oct 26, 2012 at 14:58
  • @GeneParmesan, why regex for such a simple task? – Ash Burlaczenko Commented Oct 26, 2012 at 14:59
  • @GeneParmesan - That was my first thought, but by the time the string exists in Javascript it already has no backslash characters to match with the regex! – Andrzej Doyle Commented Oct 26, 2012 at 15:02
  • Too bad file systems don't just use '/' instead ... – Gene Parmesan Commented Oct 26, 2012 at 15:06
  • If you are passing the string from the server to the client, does the client get the wrong string to begin with or is the string manipulated after? – Gene Parmesan Commented Oct 26, 2012 at 15:14
 |  Show 1 more ment

3 Answers 3

Reset to default 3

Using a single regex replace (instead of many) is quicker and avoids errors related to the replace order.

console.info('c:\build\target'); // cuild   arget

console.info(escape('c:\build\target')); // c:\build\target

function escape(str) {
  return str.replace(/[\b\f\n\r\t\v\0\'\"\\]/g, match => {
    return {
      '\b': '\\b',
      '\f': '\\f',
      '\n': '\\n',
      '\r': '\\r',
      '\t': '\\t',
      '\v': '\\v',
      '\0': '\\0',
      '\'': '\\\'',
      '\"': '\\\"',
      '\\': '\\\\'
    }[match]
  })
}

You could try:

string = "c:\build\target";
string = string.replace(/[\n]/g,'\\n');
string = string.replace(/[\r]/g,'\\r');
string = string.replace(/[\t]/g,'\\t');
string = string.replace(/[\b]/g,'\\b');
string = string.replace(/[\f]/g,'\\f');
console.log(string);​

But there maybe some other problems, because you have to catch all the other special chars

The simplest way would be to escape every backslash (so it's interpreted as a literal backslash).

If you can't do this on the remote system itself, you'll need to do this as the data is being parsed in Javascript. Obviously once you already have a Javascript string, the backslashes are already missing.

So at the point where you go from "remote string" (which includes a \ character followed by a b) to "Javascript string" (which includes a \b unprintable character), you'll need to keep an eye out for backslashes. And ensure they're passed through as a literal backslash (i.e. escaped), rather than as an escape prefix.

发布评论

评论列表(0)

  1. 暂无评论