Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #ja #display #snippet #text #string
ADD COMMENT
Topic
Name
1+9 =