Nothing
Nothing

Reputation: 25

Vim regex search not working

Having these lines of code:

 window.addEventListener('keydown', function(ev) {                                                                                                                                         
     keydown(ev.key, ev.keyCode);                                                                                                                                                          
 });

And attempting substitution on the first one with the following regex:

:s/addEventListener('\v(\w*)', function(ev)/on\1 = ev =>/g 

It says the following:

E486: Pattern not found: addEventListener('\v(\w*)', function(ev)

Why does that regex not match?

Upvotes: 0

Views: 440

Answers (2)

Peter Rincker
Peter Rincker

Reputation: 45087

You are using very magic, \v, so you need to escape your other parentheses.

:s/addEventListener('\v(\w*)', function\(ev\)/on\1 = ev =>/g

It is often a good idea to use \v at the beginning of your pattern so escaping is consistent through the pattern.

:s/\vaddEventListener\('(\w*)', function\(ev\)/on\1 = ev =>/g

Maybe even not using \v, since you have many parens to match against (it's shorter too!).

:s/addEventListener('\(\w*\)', function(ev)/on\1 = ev =>/g

You may want to use traces.vim to preview substitutions or using NeoVim's 'inccommand'. Also, newer versions (8.1.0271+) of Vim will match search patterns in side of :s, :g, and :v commands.

For more help see:

:h /\v
:h /magic
:h 'incsearch'

Upvotes: 1

tif
tif

Reputation: 1484

What is the purpose of \v in your RE? Is this not the search pattern that you need?

/addEventListener('\(\w*\)', function(ev)/

(Also, please not that the original code is missing a closing ')'.)

Upvotes: 0

Related Questions