html string snippet value change code example

Example: ja display snippet from text string

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