escape static method

String escape(
  1. String value
)
override

======================================================================== CONVERSION METHODS

Escapes (adds two slashes before) characters that have a special meaning in a RegExp: characters to escape are: ^$.|?*+()[]{}

Example: expect(FluentRegex.escape('a\^$.bc|?*d+-()[]{}1'), 'a\\\^\$\.bc\|\?\*d\+\-\(\)\\\{\}1');

Implementation

/// Escapes (adds two slashes before) characters that have a special meaning in a RegExp:
/// characters to escape are:   \^$.|?*+()[]{}
///
/// Example:
/// expect(FluentRegex.escape('a\\^\$.bc|?*d+-()[]{}1'),
/// 'a\\\\\\^\\\$\\.bc\\|\\?\\*d\\+\\-\\(\\)\\[\\]\\{\\}1');

static String escape(String value) {
  if (value.isEmpty) return value;
  const pattern =
      '(?:\\\\|\\^|\\\$|\\.|\\||\\?|\\*|\\+|\\-|\\(|\\)|\\[|\\]|\\{|\\})';
  return value.replaceAllMapped(RegExp(pattern), (Match match) {
    return '\\${match.group(0)}';
  });
}