简体   繁体   English

正则表达式以HTML文件中的getAttribute批量替换attribute [x]

[英]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: 在大量的HTML页面中,我需要替换所有这样的字符串:

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

with this: 有了这个:

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

where '24' can be any number. 其中“ 24”可以是任何数字。

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. 我以为可以使用Notepad ++的“查找并替换为正则表达式”来执行此操作,但是到目前为止,经过多次尝试和错误后,仍然运气不佳。 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.) (这是因为,当我升级到IE10时,我支持的网页上的所有文本都乱七八糟。看来XML属性的定义可以是任意顺序,而IE的较新版本将它们重新排序。我现在需要按名称而不是索引引用属性。)

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! http://regexr.com?33b0j <-观看实际操作!

You should use some wildcard replace tools in some text editor. 您应该在某些文本编辑器中使用一些通配符替换工具。 I personally use VS. 我个人使用VS。 MS-Office programs such as word has that possibility too. Word等MS-Office程序也有这种可能性。

Find what: \\.attribute\\[(\\d+)\\]\\.value; 查找内容: \\.attribute\\[(\\d+)\\]\\.value;
Replace with: .getAttribute\\("attr" + \\1\\); 替换为: .getAttribute\\("attr" + \\1\\);

(I had to escape the brackets in the replace value for my version of Notepad++ to) (我必须将我的Notepad ++版本的替换值中的方括号括起来)

Tested with notepad++ 经记事本++测试

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

replaced with 替换为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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