user985675
user985675

Reputation: 291

Vimscript: how write regex in if statement

I can search a regex with no problem, e.g.
/["']
but in an if statement it fails, e.g.

:let c = "w"
:if c !~ ["']
:echo "OK"
:endif

producing these error messages

E114: Missing quote: "']
E15: Invalid expression: c !~ ["']

I've tried it umpteen different ways, bristling with backslashes, single and double quotes and 'very magic', but all I get is variation in the error messages. How should I write this 'if' statement?

Upvotes: 1

Views: 502

Answers (2)

Kent
Kent

Reputation: 195039

you should use the regex as string, try

:if c!~"[\"']"

Upvotes: 1

Leroy
Leroy

Reputation: 544

I've personally had some problems with searching for [ and an opposing ] with Vim. Often times you can search either one independently with no problems, but when you combine both together, they need to be escaped.

So, try this: \["'\] and see if that works.

Upvotes: 0

Related Questions