I am having the following html :
<div id="iView" name="iView" contenteditable="true" style="width: 545px; height:185px; border: 2px solid green;"><p>jgfj</p>hello this</div>
<input type="button" value="click" onClick="getEditorText();" />
And my Script goes here :
function getEditorText(){
var viewContent = document.getElementById('iView').innerHTML;
alert(viewContent);
//alert(viewContent.replace(/<([^>]+)>/ig,"")); nothing works
//alert(viewContent.replace(/(<\S([^>]+)>)/ig,"")); nothing works
//alert(viewContent.replace(/<[^>]+>/ig,"")); nothing works
//alert(viewContent.replace(/<\/?[^>]+(>|$)/g, "")); nothing works
//alert(viewContent.replace(/<(?:.|\n)*?>/gm, '')); nothing works
}
In my first alert it is omitting tags..
But when i include tags dynamically ..it is not omitting the tags..so let me know how to strip the html tags dynamically using javascript , not jquery?
Modified version of your code that should work properly:
function getEditorText() {
var el = document.getElementById('iView'),
viewContent = el.innerText || el.textContent;
viewContent = viewContent.replace(/<([^>]+)>/ig,"")
.replace(/(<\S([^>]+)>)/ig,"")
.replace(/<[^>]+>/ig,"")
.replace(/<\/?[^>]+(>|$)/g, "")
.replace(/<(?:.|\n)*?>/gm, '');
el.innerHTML = viewContent;
}
Note that Firefox needs textContent
instead of innerText
.
Use innerText
instead of innerHTML
. Demo
您可以使用此正则表达式去除html标签。
var strippedString=viewContent.replace(/<(?:.|\n)*?>/gm, '');
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.