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
-
1Please take care to format your code so it's readable. I've fixed it for you in this question.Rory McCrossan– Rory McCrossan2016年12月07日 22:16:01 +00:00Commented 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".Heretic Monkey– Heretic Monkey2016年12月07日 22:19:01 +00:00Commented Dec 7, 2016 at 22:19
-
thanks for edit my question... i am newbie on stackoverflowSisi Sajah– Sisi Sajah2016年12月08日 04:29:08 +00:00Commented Dec 8, 2016 at 4:29
1 Answer 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>
Sign up to request clarification or add additional context in comments.
Comments
lang-js