fixQuotes property

string get fixQuotes

Fix the whitespaces in a string by removing spaces after opening a quote and before closing a quote.

Implementation

string get fixQuotes {
  if (isBlank) {
    return blankIfNull;
  }

  /// use regex to remove spaces after opening quotes and before closing quotes.
  /// The pattern reads as "quote, any number of spaces, stuff that's not a quote (captured), followed by any number of spaces and a quote. The replacement is just the thing you captured in quotes.
  string value = replaceAllMapped(RegExp(r'"(\s*)([^"]+)(\s*)"'), (match) => ' "${match.group(2)?.trimAll}" ');

  // also do the same with single quotes
  value = value.replaceAllMapped(RegExp(r"'(\s*)([^']+)(\s*)'"), (match) => " '${match.group(2)?.trimAll}' ");

  return value.trimAll;
}