flexAny static method

String flexAny({
  1. required List<String> keywords,
  2. bool searchWords = false,
})

Returns a regex String matching with any word from the keywords but it does not need to be the perfect word like with any.

Set searchWords true for a word search instead of keyword's characters search.

Implementation

static String flexAny({
  required List<String> keywords,
  bool searchWords = false,
}) {
  String regex = r'(';

  for (var keyword in keywords) {
    if (searchWords) {
      // The word is in the string.
      regex += r'\b' + keyword;
    } else {
      // Keyword's characters are in the string.
      regex += keyword;
    }

    if (keyword != keywords.last) {
      regex += r'|';
    }
  }

  return regex + r')';
}