Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex all character after first position in Dart

I’m trying to achieve this J*** D** from John Doe string but my current code output is **** ***.
Here is my current code:

void main() {
  String txt = 'John Doe';
  String hideStr = txt.replaceAll(RegExp(r'\S'), '*');
  print(hideStr);
}

any suggestion?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use negative look behind to exclude the character at start and after white-space

void main() {
    String txt = 'John Doe';
    String hideStr = txt.replaceAll(RegExp(r'(?<!^|\s)[^\s]'), '*');
    print(hideStr);
}

Because "J" and "D" sit after start of text and a space, the regex won’t match it

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading