【问题标题】:Using `Regexp` - how to make list from string array? [closed]使用`Regexp` - 如何从字符串数组中创建列表? [关闭]
【发布时间】:2016-02-22 11:38:44
【问题描述】:

我有一个从后端服务器获取的数组。但我想使用'ul'将该数组转换为list。如何使用regexp 清理数组并将它们转换为单个清除数组?

这是我得到的数据:

[
  "1. Submittals in progress\n2. Structural works in progress in S2 & S3\n3. Structural works in progress in N4.\n4. Childrens Mall pedestal and steel fabrication.",
  "",
  "1. Procurement of activities in progress.\n2. External works for mist pool in progress\n3. MEP 1st & 2nd fix in progress\n4. Block work and grade slab in progress "
]

结果应该是:(有一个标记为\n 用于休息。以及empty 两个数组之间的字符串。

var array = [
"Procurement of activities in progress.",
"Structural works in progress in S2 & S3",
"Structural works in progress in N4.",
"Childrens Mall pedestal and steel fabrication."
"Procurement of activities in progress."
"External works for mist pool in progress",
"MEP 1st & 2nd fix in progress",
"Block work and grade slab in progress"

]

【问题讨论】:

  • 你有文字 \n 还是只是换行符?
  • 我们可以将其视为换行

标签: javascript jquery arrays regex


【解决方案1】:

假设这是输入:

arr=[
  "1. Submittals in progress\n2. Structural works in progress in S2 & S3\n3. Structural works in progress in N4.\n4. Childrens Mall pedestal and steel fabrication.",
  "",
  "1. Procurement of activities in progress.\n2. External works for mist pool in progress\n3. MEP 1st & 2nd fix in progress\n4. Block work and grade slab in progress "
]

您可以使用以下方法获取输出数组:

var array = arr.filter(Boolean).join('\n').replace(/^\d+\.\s+/gm, '').split('\n');

filter(Boolean) 用于从输入中消除空数组。

输出:

[
 "Submittals in progress",
 "Structural works in progress in S2 & S3",
 "Structural works in progress in N4.",
 "Childrens Mall pedestal and steel fabrication.",
 "Procurement of activities in progress.",
 "External works for mist pool in progress",
 "MEP 1st & 2nd fix in progress",
 "Block work and grade slab in progress "
]

【讨论】:

  • 你熟悉angularjs - 因为我在filter 中使用它。但是当数组有重复的值时,我得到了错误。所以我尝试使用track by index - 但当时过滤器不起作用
  • 对不起,我对 angularjs 不熟悉
【解决方案2】:

你需要

  • 遍历第一个数组的项,
  • 用新行分割每个项目以创建一个数组,
  • 将项目连接到 newArray

这里是代码

var arr1 = [
  "1. Submittals in progress\n2. Structural works in progress in S2 & S3\n3. Structural works in progress in N4.\n4. Childrens Mall pedestal and steel fabrication.",
  "",
  "1. Procurement of activities in progress.\n2. External works for mist pool in progress\n3. MEP 1st & 2nd fix in progress\n4. Block work and grade slab in progress "
];

var newArray = [];
arr1.forEach( function(value){
  if ( value && value.length > 0 )
  {
     value = value.split(" ").slice(1).join(" ");
     var itemsInArray = value.split("\n");
     if ( value && value.length > 0 && itemsInArray.length > 0 )
     {
       newArray = newArray.concat( itemsInArray );
     }
    }
} );
console.log( newArray );

【讨论】:

  • 在数组中连接后,我看到index 数字重复。虽然这一切都附加到 html,但这会产生问题。
【解决方案3】:

不要写太长的代码。使用以下短而甜的。

var text = [
  "1. Submittals in progress\n2. Structural works in progress in S2 & S3\n3. Structural works in progress in N4.\n4. Childrens Mall pedestal and steel fabrication.",
  "",
  "1. Procurement of activities in progress.\n2. External works for mist pool in progress\n3. MEP 1st & 2nd fix in progress\n4. Block work and grade slab in progress "
]; 
var rawData = text.join('\n').split('\n').filter(function(index) {
    return index !== '' ;
});;
console.log(rawData);

我知道它没有优化但可以工作。

运行此代码并观察输出。

【讨论】:

  • 不过,这不会删除数字。
  • 哦...没有正确观看输出
猜你喜欢
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
相关资源
最近更新 更多