replace method

Quoted replace (
  1. Node string,
  2. Quoted pattern,
  3. Node replacement,
  4. [Node flags]
)

Replaces a text within a string.

Parameters: string: The string to search and replace in. pattern: A string or regular expression pattern to search for. replacement: The string to replace the matched pattern with. flags: (Optional) regular expression flags. Returns: a string with the replaced values. Example: replace("Hello, Mars?", "Mars?", "Earth!"); replace("One + one = 4", "one", "2", "gi"); replace('This is a string.', "(string).$", "new $1."); replace(~"bar-1", '1', '2'); Result: "Hello, Earth!"; "2 + 2 = 4"; 'This is a new string.'; bar-2;

Implementation

Quoted replace(Node string, Quoted pattern, Node replacement, [Node flags]) {
  //string, replacement, flags is Quoted ('value') or Keyword (value)

  final String flagsValue = flags != null ? flags.value : '';
  final re = MoreRegExp(pattern.value, flagsValue);

  final replacementStr =
      (replacement is Quoted) ? replacement.value : replacement.toCSS(null);
  final result = re.replace(string.value, replacementStr);

  final quote = (string is Quoted) ? string.quote : '';
  final escaped = (string is Quoted) ? string.escaped : false;
  return Quoted(quote, result, escaped: escaped);

//2.4.0 20150331
//  replace: function (string, pattern, replacement, flags) {
//      var result = string.value;
//      replacement = (replacement.type === "Quoted") ?
//          replacement.value : replacement.toCSS();
//      result = result.replace(new RegExp(pattern.value, flags ? flags.value : ''), replacement);
//      return new Quoted(string.quote || '', result, string.escaped);
//  },
}