如何仅替换 \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'
最简单的方法是使用原始字符串:
var output = text.replaceAll(r'\N', ' ');
print ("$output");
r
字符串前面的 '\N'
确保 \
用作文字,而不是特殊字符。