function getSnippet(text, length) {
var rx = new RegExp("^.{" + length + "}[^ ]*");
return rx.exec(text)[0];
}
console.log(getSnippet("get snippet text using javascript?", 1)); // get
console.log(getSnippet("get snippet text using javascript?", 3)); // get
console.log(getSnippet("get snippet text using javascript?", 10)); // get snippet
console.log(getSnippet("get snippet text using javascript?", 11)); // get snippet
<html>
<head>
<title>test</title>
<!-- load highlight.js css & js -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js" type="text/javascript"></script>
</head>
<body>
<!-- set up your tag which want to highlight -->
<pre><code class="xml">
<script src='URL type='text/javascript'></script><script>functionName();</script>
</code></pre>
<script>
// initialize highlight
hljs.initHighlightingOnLoad();
</script>
</body>
</html>