我試圖用NSRegularExpression
\[word\]
更換[word]
:NSRegularExpression添加轉義字符
NSRegularExpression *metaRegex = [NSRegularExpression regularExpressionWithPattern:@"([\\[\\]])"
options:0
error:&metaRegexError];
NSString *escapedTarget = [metaRegex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, string.length)
withTemplate:@"\\$1"];
但是這個輸出是$1word$1
。你會認爲第一個\
會逃脫第二個\
字符,但它看起來像逃避$
字符......我怎麼告訴它逃離\
而不是$
?