尝试在javascript中的字符串中匹配年份

时间:2018-07-12 16:43:40

标签: javascript html json regex

我希望能够接受一个字符串数组:

这是我正在使用的模拟数据。

const archives = {
    "data": [{
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Employee Engagement Summary",
                "description": "Report description goes here",
                "creation_date": "March 21, 2018",
                "date_range": "03/01/2018-05/15/2018",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 1",
                "description": "Report description goes here",
                "creation_date": "March 21, 2017",
                "date_range": "03/01/2017-05/15/2017",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 2",
                "description": "Report description goes here",
                "creation_date": "March 21, 2016",
                "date_range": "03/01/2016-05/15/2016",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 3",
                "description": "Report description goes here",
                "creation_date": "March 21, 2015",
                "date_range": "03/01/2015-05/15/2015",
                "data_sets": "Facility A, Nursing Department"
            }
        }
    ]
};

export default archives;

上面的代码具有我正在使用的真实模拟数据。

然后从中提取年份。因为我希望能够遍历这些年并将它们放在列表中

<ul>
  <li>2018</li>
  <li>2017</li>
  <li>2016</li>
  <li>2015</li>
  <li>2014</li>
  <li>2013</li>
  <li>2012</li>
</ul>

5 个答案:

答案 0 :(得分:1)

只需四位数匹配,至少对于您的情况而言

(str => str.match(/\d{4}/)[0])

var dateStrings = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
];
var years = dateStrings.map(str => str.match(/\d{4}/)[0]);
var ul = years.reduce((ul, year) => {
  var li = document.createElement('li');
  li.innerHTML = year;
  ul.appendChild(li);
  return ul;
}, document.createElement('ul'));

document.body.appendChild(ul);

答案 1 :(得分:1)

尝试一下:

var archives = {
    "data": [{
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Employee Engagement Summary",
                "description": "Report description goes here",
                "creation_date": "March 21, 2018",
                "date_range": "03/01/2018-05/15/2018",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 1",
                "description": "Report description goes here",
                "creation_date": "March 21, 2017",
                "date_range": "03/01/2017-05/15/2017",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 2",
                "description": "Report description goes here",
                "creation_date": "March 21, 2016",
                "date_range": "03/01/2016-05/15/2016",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 3",
                "description": "Report description goes here",
                "creation_date": "March 21, 2015",
                "date_range": "03/01/2015-05/15/2015",
                "data_sets": "Facility A, Nursing Department"
            }
        }
    ]
};

var yearsRange = archives.data.map(obj => obj.attributes.date_range);

var yearArr= [];

yearsRange.filter(item => {
   yearArr.push(item.split('/')[2].split('-')[0]);
});

console.log("yearArr", yearArr);

答案 2 :(得分:0)

var str = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
]; 

//Array to hold the years
let years = [];

//Iterate your date collection
for(let i = 0; i < str.length; i++){
    //Split the string on the comma
    const splitter = str[i].split(",");

    //If the split produces more than one string value, meaning we have a formatted date
    if(splitter.length > 1){
       //Replace any non digit characters and trim whitespace
       let year = splitter[1].replace(/\D/g,'').trim();
       //push year value into new array
       years.push(year);

    }
}

//View the results in console
console.log(years);

答案 3 :(得分:-2)

如果您的字符串始终采用相同的格式,则可以在,上进行拆分。我使用正则表达式/[^0-9]/gi用非字符''替换了一年中非数字的任何内容。这样可以防止2017f之类的东西包含f。

var dates = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
]; 

const years = [];
for(const date of dates){
  const year = date.split(',')[1].replace(/[^0-9]/gi, '').trim();
  years.push(year);
}

console.log(years);

答案 4 :(得分:-2)

使用匹配项并检查4位数字:/\d{4}/

var str = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
];

let ul = document.querySelector('ul')

str.forEach(date => {
  let matches = date.match(/\d{4}/)
  matches.length > 0 ? (ul.innerHTML += `<li>${matches[0]}</li>`) : null
})
<ul></ul>