用 document.getElementById("something") 替换 document.all.something 的正则表达式

regular expression to replace document.all.something with document.getElementById("something")

我有许多为 IE 项目编写的旧 javascript 文件需要转换。他们使用 document.all.somename 而不是 document.getElementById("somename")。我一直在手动转换这些,我的手都抽筋了。有没有办法用正则表达式做到这一点?

使用捕获组捕获 somename 部分。

> "bar document.all.somename foo".replace(/\bdocument\.all\.(\w+)\b/g, 'document.getElementById("")')
'bar document.getElementById("somename") foo'