Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

question from:https://stackoverflow.com/questions/66057230/how-to-prevent-string-auto-escaping

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
122 views
Welcome To Ask or Share your Answers For Others

1 Answer

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, '');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...