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