function getWordStr(str) {
return str.split(/\s+/).slice(0,10).join(" ");
}
//TO CALL THIS FUNCTION
var fullStr = "We can get first 10 words form this string using the above function";
var finalStr = getWordStr(fullStr);
console.log(finalStr);
The code snippet can be used to get first 10 words from a string. You can set start and end points in the 'getWordStr' function. We have applied a start point form 0 and end point to 10.
0 Comments