0

how to replace or change specific text to new html?

Old HTML:

<div id="test"> 
 <p>
 [b] lorem ipsum [/b]
 </p>
 <p>
 [textarea] lorem ipsum [/textarea] 
 </p>
</div>

New HTML:

<div id="test">
 <p>
 <b>lorem ipsum</b>
 </p>
 <p>
 <textarea>lorem ipsum</textarea>
 </p>
</div>
Force_Tag = [
 '[b]','<pre>',
 '[/b]','</b>',
 '[textarea]','<textarea>',
 '[/textarea]','</textarea>',
 // ...
]
Rory McCrossan
338k41 gold badges322 silver badges353 bronze badges
asked Dec 7, 2016 at 22:13
3
  • 1
    Please take care to format your code so it's readable. I've fixed it for you in this question. Commented Dec 7, 2016 at 22:16
  • Welcome to Stack Overflow! Please read How to Ask. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". Commented Dec 7, 2016 at 22:19
  • thanks for edit my question... i am newbie on stackoverflow Commented Dec 8, 2016 at 4:29

1 Answer 1

1

You can use String.prototype.replace() with RegExp /(\[)|(\])/g to capture "[", "]", replace first capture group with "<", second capture group with ">", set .innerHTML of elements to replacement strings

<div id="test">
 <p>
 [b] lorem ipsum [/b]
 </p>
 <p>
 [textarea] lorem ipsum [/textarea]
 </p>
</div>
<script>
 for (let p of document.querySelectorAll("#test p")) {
 p.innerHTML = p.innerHTML.replace(/(\[)|(\])/g, function(match, p1, p2) {
 if (p1) return "<";
 if (p2) return ">"
 })
 }
 </script>

answered Dec 7, 2016 at 22:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.