Dart 用正则表达式替换转义字符

问题描述 投票:0回答:1

如何仅替换 \N?

void main() {
     var text= 'Here is an N Here is an N escaped \N replace just the last one';
    
      var text1 = text.replaceAll(r'\N', ' ');
      var text2 = text.replaceAll('\N', ' ');
      var text3 = text.replaceAll('\\N', ' ');
      var text4 = text.replaceAll('\N', ' ');
      var text5 = text.replaceAll(RegExp(r'\\N'), ' ');
      
      print ("$text1");
       print ("$text2");
       print ("$text3");
       print ("$text4");
      print ("$text5");
    }
    Input text literal: 'Here is an N Here is an N escaped N Remove just the last one'
    'Here is an N Here is an N escaped N Remove just the last one'
    'Here is an   Here is an   escaped   Remove just the last one'
    'Here is an N Here is an N escaped N Remove just the last one'
    'Here is an   Here is an   escaped   Remove just the last one'
    'Here is an N Here is an N escaped N Remove just the last one'
regex flutter dart
1个回答
0
投票

最简单的方法是使用原始字符串:

var output = text.replaceAll(r'\N', ' ');

print ("$output");

r
字符串前面的
'\N'
确保
\
用作文字,而不是特殊字符。

© www.soinside.com 2019 - 2024. All rights reserved.