0

I would like to replace a string between 2 other strings in dart, for example :

var str="<!-- cells -->test<!-- cells -->";

the delimiters around will not change and will be always the same.

How to replace 'test' by 'test2' for example ?

2 Answers 2

4
  var str = "<!-- cells -->test<!-- cells -->";
  var replace = 'foo';
  var counter = 0;

  final result = str.replaceAllMapped(RegExp(r'(<!-- )(.*?)( -->)'), (m) {
    return '${m[1]}$replace${counter++}${m[3]}';
  });

  print(result);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you but this solution replaces the string around and not the string "test"
I see. I misread the question. Changing the RegExp to r'( -->)(.*?)(<!-- )' should do.
0

finally I found a solution quickly: replaceAllMapped

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.