简体   繁体   中英

Regular Expression, replace specific letter between html tags

I am having problems with regular expression replacement on html document. I need to find some specific letter in html text nodes and replace it with a different letter.

Lets say for example I want to replace letter "e" with letter "x".

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.

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:

 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. And this is my solution for my own question.

Need to download Sublime text editor. All files and manuals you can be find here Sublime text 2 It has nice and claver "find and replace" tools build in.

Than after long hours learn I find " regReplace " plugin is best for for this needs. It crates separate regExp rules and run it one by one over same selection.

To download plugin you need to read some pages on plugin developer website RegReplace Sublime plugin

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.

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