简体   繁体   English

如何解析包含\\ n的变量?

[英]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.

相关问题 如何迭代包含 2D arrays 的 object 而不会得到 O(n)^3? - How do I iterate through an object containing 2D arrays without getting O(n)^3? 如何从XMLHttpRequest解析javascript中的该值并将其用作变量? - How do I parse this value in javascript from XMLHttpRequest and use it as a variable? 如何获取JSON.Stringify正确解析变量? - How do I get JSON.Stringify to parse variable correctly? 我如何解析json值并将id放入变量 - how do i parse json value and get id into variable 如何使用包含布尔值的全局变量来更改谷歌表格脚本中的其他变量? - How do I use global a global variable containing a boolean to change other variable in google sheets script? 如何在 JavaScript 中解析 JSON 字符串数组的前 n 行? - How do I parse the first n rows of a JSON String array in JavaScript? 如何从 object 属性中获取一段文本(带有 `\n`)并将其解析为段落? - How do I take a block of text (with `\n`) from an object property and parse it as paragraphs? 如何解析包含函数的 JSON 字符串? - How can I parse a JSON string containing functions? 如何解析包含冒号的JSON对象 - How can I parse a JSON object containing a colon 如何解析包含PHP对象数组的JSON对象? - How can I parse a JSON object containing a PHP array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM