简体   繁体   中英

How to prevent string auto escaping?

I have a variable var a = '\204444' , and I want to remove \ . I need the result to be 204444 .

I tried using a.replace(/\\/g, '') , but the result is not what I wanted.

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


If you don't you can do this ( but that doesn't work if you have leading 0s )

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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