简体   繁体   中英

Sublime text find and replace with regex to convert jquery live() event to on()

I am doing an upgrade to the latest jQuery and I have a large number of js files that I need to update. Mostly the conversion of the live() event to on() (there are hundreds)

In Sublime text, I am wondering how to use the find and replace with regex to achieve the following.

Find (using regex to select the class name):

$('.class-name').live('click'

Replace with:

$('#content').on('click', '.class-name'

Edit: My attempt looks something like this, although I know it's not quite right...

find:

$('(.*?)').live('click'

replace with

$('#content').on('click', '$1'

Does anyone know how to do this?

You basically had it. You just needed to escape the special characters in your find expression, as follows:

Find: \\$\\('(\\..*?)'\\)\\.live\\('click'

Replace: $('#content').on('click', '$1'

I threw in a literal . at the beginning of the classname capture assuming you only wanted to apply this to class locators.

The one thing I'm not sure about is whether you need to restrict the characters matched by the classname expression. If so, it's a simply matter of replacing the . character with a character class such as [\\w-.] or whatever can be in a class name.

You can use jQuery migrate . Include it after the core jquery file. This way you will not see any errors like Uncaught TypeError: Object [object Object] has no method 'live'

http://code.jquery.com/jquery-migrate-1.2.0.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM