Q:

What is the equalient of JavaScript's "s.replace(/[^\w]+/g, '-')" in Dart language?

I am trying to get the following working code in JavaScript also working in Dart.
https://jsfiddle.net/8xyxy8jp/1/
var s = "We live, on the # planet    earth";
var results = s.replace(/[^\w]+/g, '-');

document.getElementById("output").innerHTML = results;

Which gives the output
We-live-on-the-planet-earth

I have tried this Dart code
void main() {
    print( "We live, on the # planet    earth".replaceAll("[^\w]+","-"));
}

But the output becomes the same.
What am I missing here?

A:

If you want replaceAll() to process the argument as regular expression you need to pass a RegExp instance. I usually use r as prefix for the regex string to make it a raw string where not interpolation ($, \, ...) takes place.
main() {
  var s = "We live, on the # planet    earth";
  var result = s.replaceAll(new RegExp(r'[^\w]+'), '-');
  print(result);
}

Try it in DartPad

