Search code examples
regexcheerio

using cheerio to get attributes using a regex


I would like to get the attributes of a tag using a pattern. Consider something like so

<foo id="136E2A751268FFDFD3EB6CB6FE91D88F" 
    captionText-0="bla bla" 
    captionText-1="bada bing bada boom" 
    httpUri-0="https://path/to/server0" 
    httpUri-1="https://path/to/server2">foo 1-2</foo>

using cheeriojs, something like the following (which, of course, doesn't work)

const attribs = [
    'captionText',
    'httpUri'
]

const tag = {}
attribs.forEach(a => {
    const re = `${a}-[0-9]+`
    const attr = $(e).attr(/re/)
    if (attr) {
        tag[re] = attr
    }
})

suggestions?


Solution

  • If you're simply matching against a list, then:

    // cheeiro output of element.attribs
    var attribs = {
      'captionText-1': '1',
      'captionText-2': '2',
      'httpUri-3': 3,
      'httpUri-4': 4
    };
    // list of values to match against
    var match_list = ['captionText', 'httpUri'];
    // holds matched key/value attributes
    var tag = {};
    // loops through attribs object
    for (var key in attribs) {
      // returns boolean, partial matches current attribute against each value in match list
      // var is_matched = match_list.some(function(item) {return key.indexOf(item) > -1;});
      var is_matched = match_list.some(function(item) {return key.match(new RegExp('^' + item + '-[0-9]+', 'gi'));});
      if (is_matched) {
        tag[key] = attribs[key];
      }
    }