Vim equivalent of `preg_quote()` and `quotemeta`
In vimscript I can regexp-match stuff with matchstr()
and matchlist()
But what 开发者_C百科if I have to append to the pattern a user-provided string?
How can I quote meta characters in that?I don't think there's anything that does that, but you could use a magicness modifier which might be good enough. Let's say you have the following expression:
let pattern = '\<'.word.'\>'
if word
is a user-supplied string, then (if I understand correctly) you'd like to ignore all special modifiers in it. To do that, use "very nomagic mode". By putting a "\V" in front of the pattern, you make the pattern behave almost like a literal string. Afterwards, put "\m" to restore normal behaviour. In this case:
let pattern = '\<\V'.word.'\m\>'
Note, however, that this is not exactly what you're asking for. If the user enters foo(*bar)
, it will work just fine, but if it's foo(\*bar)
, it won't, because the \*
pair is being interpreted as a star modifier. If you don't know about these modes, try :help /\V
to get a better idea of how magicness works.
精彩评论