I have a paragraph which has HTML tags in it. I need to restrict it to 100 characters after the parse of HTML. For example
Hello<a href ="#"> World </a>
If my length check is for 8, then my HTML should be Hello Wo
where Wo
should be an anchor tag. There can be any other tags. Any help would be appreciated.
$(document).ready(function(){ var $html=$('<span></span>'); $html.append('Hello<a href ="#"> World </a>'); var text=$html.text(); console.log("Text :: "+text); console.log("Length :: "+text.length); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Using this way you can create html elment virtually then text()
will parse the text content from html.
You can use a REGEX expresion when getting the innerHTML
Then count the characters.
Example:
var regex = /(<([^>]+)>)/ig; var body = 'Hello<a href ="#"> World </a>'; var result = body.replace(regex, ""); console.log(result + ' ' + result.length);
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.