censorWords method

String censorWords({
  1. List<String>? censors,
})

We define a list of bad words (badWords) that you want to censor.

We create a regular expression pattern by joining the bad words with the word boundary anchors (\b) and using the | (OR) operator to match any of the bad words. This ensures that only complete words are censored, not partial matches.

We use the replaceAllMapped method to replace each occurrence of a bad word in the text with asterisks of the same length as the bad word.

Finally, we demonstrate how to use the censorBadWords function with a sample text, and it prints the text with the bad words replaced by asterisks.

Implementation

String censorWords({List<String>? censors}) {
  // List of bad words to censor
  List<String>? badWords = censors ?? Delta.data.blockedWords;
  if (badWords != null) {
    // Create a regular expression pattern for each bad word
    String pattern = badWords.map((word) => '\\b$word\\b').join('|');
    RegExp regExp = RegExp(pattern, caseSensitive: false);

    // Replace bad words with asterisks
    return replaceAllMapped(regExp, (match) => '*' * match.group(0)!.length);
  } else {
    throw ErrorDescription(
        "The method failed because you haven't pass List of censor words on which the will have to block them"
        "You have two ways to pass censor words either in [DeltaApp] or directly to extension method like 'What is your name?'.censorWords(censors:['name']) the method will return 'What is you ****?'  ");
  }
}