Reputation: 59395
I have a really weird number in my mongoDB document. I copied the whole object over to the node
console. I'm outputting it here. I'm thinking it has a combination of string \n
's and actual textual \n
's or something weird going on. Can someone tell me how to just get the number 567899876545678987654
as a string, out of the variable below?
> number
'--- \n- "567899876545678987654"\n- \n'
> JSON.stringify(number);
'"--- \\n- \\"567899876545678987654\\"\\n- \\n"'
> number.replace("\n","");
'--- - "567899876545678987654"\n- \n'
> number.replace(/\n/,"");
'--- - "567899876545678987654"\n- \n'
> number.replace(/-/,"");
'-- \n- "567899876545678987654"\n- \n'
Upvotes: 2
Views: 108
Reputation: 44259
Why not use a regex for all digits instead of trying to remove the rest?
actualNumber = number.match(/\d+/);
As T.J. Crowder points out, actualNumber
will now be an array containing the number (if there was one). As he suggests, this will safely retrieve the actual number:
var m = number.match(/\d+/);
if (m) {
actualNumber = m[0];
}
Upvotes: 5