SSS SSS - 26 days ago 9
Dart Question

Dart: Replace every letter in a string with an asterix using regxp

I was trying to replace every letter in a string with an asterix using Dart and am having some difficulty. In Javascript it is:

'test'.replace(/./g, '*');


So in Dart, I tried the following:

'test'.replaceAll(new RegExp(r'/.'), '*');
'test'.replaceAll(new RegExp(r'\/.'), '*');
'test'.replaceAll(new RegExp(r'/./'), '*');
'test'.replaceAll(new RegExp(r'\/.\/'), '*');


I obviously don't quite understand how this should work as cannot seem to make this work.

Can anyone advise the correct way to do this with a short explanation?

Thanks.

Answer Source
var regex = new RegExp(r'[a-zA-Z]')

var replaced = 'some string / with $ other = chars'.replaceAll(regex, '*');

DartPad example