简体   繁体   English

如何防止字符串自动 escaping?

[英]How to prevent string auto escaping?

I have a variable var a = '\204444' , and I want to remove \ .我有一个变量var a = '\204444' ,我想删除\ I need the result to be 204444 .我需要结果是204444

I tried using a.replace(/\\/g, '') , but the result is not what I wanted.我尝试使用a.replace(/\\/g, '') ,但结果不是我想要的。

If you have control over the variable initialisation you can do this如果您可以控制变量初始化,则可以执行此操作

var a = String.raw`\204444`;
a.replace(/\\/g, '');

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw


If you don't you can do this ( but that doesn't work if you have leading 0s )如果你不这样做,你可以这样做(但如果你有前导 0,那就行不通了

var a = '\2304444';
a.charCodeAt(0).toString(8) + a.replace(/^./g, '');

Run this code snippet to check, it only work with string have escaped first char.运行此代码片段进行检查,它仅适用于已转义第一个字符的字符串。

 var a='\204444'; a=a.replace(a[0], a.charCodeAt(0).toString(8)) console.log(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM