简体   繁体   中英

Regex to bulk replace attributes[x] with getAttribute in HTML files

In a boatload of HTML pages, I need to replace all strings like this:

language[0].attributes[24].value;

with this:

language[0].getAttribute("attr"+24);

where '24' can be any number.

I thought I could use Notepad++'s Find and Replace with regular expressions to do this, but have had no luck so far after much trial and error. I don't want to have to do this manually.

(This is because, when I upgraded to IE10, all the text on the webpages I support went out of whack. It seems that by definition XML attributes can be in any order, and that newer versions of IE reorder them. I now need to reference the attributes by name instead of index.)

Appreciate the help.

find the following regular expression:

\.attributes\[(\d+)\]\.value

And replace it with

.getAttribute("attr"+\1)

http://regexr.com?33b0j <-- See it in action!

You should use some wildcard replace tools in some text editor. I personally use VS. MS-Office programs such as word has that possibility too.

Find what: \\.attribute\\[(\\d+)\\]\\.value;
Replace with: .getAttribute\\("attr" + \\1\\);

(I had to escape the brackets in the replace value for my version of Notepad++ to)

Tested with notepad++

language\[0\].attributes\[\s*(\d+)\s*\].value;

replaced with

language[0].getAttribute\("attr"+\1\);

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