[英]How do I parse this variable containing \n?
I have a really weird number in my mongoDB document. 我的mongoDB文件中有一个非常奇怪的数字。 I copied the whole object over to the
node
console. 我将整个对象复制到了
node
控制台。 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. 我认为它具有字符串
\\n
和实际文本\\n
的组合,或者发生了一些奇怪的事情。 Can someone tell me how to just get the number 567899876545678987654
as a string, out of the variable below? 有人可以告诉我如何从下面的变量中获取数字
567899876545678987654
作为字符串吗?
> 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'
Why not use a regex for all digits instead of trying to remove the rest? 为什么不对所有数字使用正则表达式,而不是尝试删除其余数字?
actualNumber = number.match(/\d+/);
As TJ Crowder points out, actualNumber
will now be an array containing the number ( if there was one). 正如TJ Crowder指出的那样,
actualNumber
现在将是一个包含数字的数组( 如果有的话 )。 As he suggests, this will safely retrieve the actual number: 正如他建议的那样,这将安全地检索实际数字:
var m = number.match(/\d+/);
if (m) {
actualNumber = m[0];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.