简体   繁体   English

正则表达式,替换html标签之间的特定字母

[英]Regular Expression, replace specific letter between html tags

I am having problems with regular expression replacement on html document. 我在html文档上使用正则表达式替换时遇到问题。 I need to find some specific letter in html text nodes and replace it with a different letter. 我需要在html文本节点中找到一些特定的字母,并将其替换为其他字母。

Lets say for example I want to replace letter "e" with letter "x". 例如,让我用字母“ x”替换字母“ e”。

My string may look like this: 我的字符串可能如下所示:

<div class="some class" style="display:block">some text here</div>

so after replacement it must be: 因此更换后必须是:

<div class="some class" style="display:block">somx txxt hxrx</div>

As you can see, all html structure is still the same, only text between > and < has been affected. 如您所见,所有html结构都相同,仅><之间的文本受到了影响。

Thank you for the help! 感谢您的帮助!

Since your question is tagged javascript , you can easily replace text content of your <div> without worrying about affecting tag attributes using String.prototype.replace() like this: 由于您的问题被标记为javascript ,因此您可以轻松地替换<div>文本内容,而不必担心使用String.prototype.replace()这样影响标签属性:

 var el = document.getElementById('my_div'); var html = el.innerHTML; html = html.replace(/e/g,'x'); el.innerHTML = html; 
 <div id="my_div">some text</div> 

Ok, maybe some one will find my answer useful. 好的,也许有人会觉得我的回答有用。 I try (for example) to replace all German letters to normal english char in my HTML document. 我尝试(例如)在HTML文档中将所有德语字母替换为普通英语char。 And this is my solution for my own question. 这是我对自己的问题的解决方案。

Need to download Sublime text editor. 需要下载Sublime文本编辑器。 All files and manuals you can be find here Sublime text 2 It has nice and claver "find and replace" tools build in. 您可以在此处找到所有文件和手册。Sublime text 2它内置了漂亮又聪明的“查找和替换”工具。

Than after long hours learn I find " regReplace " plugin is best for for this needs. 经过长时间的学习,我发现“ regReplace ”插件最适合这种需求。 It crates separate regExp rules and run it one by one over same selection. 它创建单独的regExp规则,并在同一选择上一一运行。

To download plugin you need to read some pages on plugin developer website RegReplace Sublime plugin 要下载插件,您需要阅读插件开发者网站RegReplace Sublime插件上的一些页面。

After you crate rules for each letter, you can use build in " find " tool to select all between tag using: 在为每个字母创建规则之后,您可以使用内置的“ 查找 ”工具,使用以下命令在标记之间进行选择:

>.*?<

then you press " find all " and apply all regReplace rules to selected text. 然后按“ 查找全部 ”,然后将所有regReplace规则应用于所选文本。

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

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