在 Notepad++ 中查找并替换为特定文本

Find and replace in Notepad++ with specific text

我有 xml 文件,我需要在其中对元素进行一些更改。

<someElement name="Sometext">

我想用这样的属性修改上面的内容

<someElement name="Sometext" newattribute="Sometext_8c54">

在上面我只想复制 name 属性并创建一个 newattribute 其值为 具有 _4digitrandomhexadecimal

的名称 属性

如何在notepad++中实现这个

如果您使用 EmEditor,请按 Ctrl + H 调出 替换对话框,输入:

查找: <(.*?)name="(.*?)">

替换为\J "<name=\"\" newattribute=\"_" + (Math.floor(Math.random()*16)).toString(16) + (Math.floor(Math.random()*16)).toString(16) + (Math.floor(Math.random()*16)).toString(16) + (Math.floor(Math.random()*16)).toString(16) + "\">"

设置正则表达式选项,然后单击全部替换

说明

开头的

\J替换为字符串表示是JavaScript。 </code> 和 <code> 是指向匹配字符串中第一个 (.*?) 和第二个 (.*?) 的反向引用。 (Math.floor(Math.random()*16)).toString(16)是一个随机的十六进制字符。

参考文献:http://www.emeditor.org/en/howto_search_replacement_expression_syntax.html