Skip to content Skip to sidebar Skip to footer

Javascript Regex: Ignore Subgroup When Matching

I'm trying to match a string with an optional part in the middle. Example strings are: 20160131_abc.pdf 20160131_abc_xx.pdf 20160131_def.pdf The result should include the name of

Solution 1:

You can restrict the def part with a (?=\.pdf) lookahead that will require .pdf to appear right after def if there is def before the .pdfand add the optional group (?:_xx)? before the .pdf:

[0-9]{8}_(abc|def(?=\.pdf))(?:_xx)?\.pdf

See the regex demo

Explanation:

  • [0-9]{8} - 8 digits
  • _ - underscore
  • (abc|def(?=\.pdf)) - Capture group 1 matching abc or def (def is only matched if .pdf follows it immediately)
  • (?:_xx)? - optional _xx part that can only appear in the match (not in the capture) if preceded with abc
  • \.pdf - literal .pdf substring

Solution 2:

You can use non-capturing groups in the regex and then "implode" the match results:

var re = /([0-9]{8}_)(abc|def)(?:_xx)?(\.pdf)/;
var tests = [
  '20160131_abc.pdf',
  '20160131_abc_xx.pdf',
  '20160131_def.pdf'
];
var container = document.getElementById('container');
tests.forEach(function(test){
  var match = test.match(re);
  var fileName = match.slice(1).join('');
  container.innerHTML += "test:" + test + " → ";
  container.innerHTML += fileName + "<br/>";
});

See fiddle

Post a Comment for "Javascript Regex: Ignore Subgroup When Matching"