Liad Livnat
Liad Livnat

Reputation: 7475

Javascript truncate a word around search expression

I would like your help by finding a smart truncate function that will do the following:

  1. according to a search expression will truncate the sentence to a define length,
  2. keep the search expression inside the truncated sentence.

for example:

var sentence = "As they rounded a bend in the path that ran beside the river, Lara recognized the silhouette of a fig tree atop a nearby hill. The weather was hot and the days were long. The fig tree was in full leaf, but not yet bearing fruit.
Soon Lara spotted other landmarks—an outcropping of limestone beside the path that had a silhouette like a man’s face, a marshy spot beside the river where the waterfowl were easily startled, a tall tree that looked like a man with his arms upraised";

var searchExpression = "tree";
var truncateLength = 20;

the result will be:

"the silhouette of a fig tree atop a nearby hill"

the word tree will be a part of the truncated sentence.

Are you familiar with a javascript code that capable of doing it ?

Thanks,

Upvotes: 0

Views: 216

Answers (2)

Sooraj
Sooraj

Reputation: 10567

Not sure if this is the best possible way. But here is a working code that might help.

var sentence = "As they rounded a bend in the path that ran beside the river,Lara recognized the silhouette of a fig tree atop a nearby hill. The weather was hot and the days were long. The fig tree was in full leaf, but not yet bearing fruit.Soon Lara spotted other landmarks—an outcropping of limestone beside the path that had a silhouette like a man’s face, a marshy spot beside the river where the waterfowl were easily startled, a tall tree that looked like a man with his arms upraised";
var searchExpression = "tree";
var truncateLength = 20;

var index = sentence.indexOf("tree");

var leftIndex = index - (truncateLength);
var rightIndex = index + (truncateLength);
var result = sentence.substring(leftIndex - searchExpression.length , rightIndex + searchExpression.length);
alert(result);

Upvotes: 1

Manticore
Manticore

Reputation: 1284

Have a look at this. I embedded georg's regex and made a javascript function out of it.

https://jsfiddle.net/94xh1umx/

Javascript:

function truncate(sentence, searchExpression, truncateLength) {
  var pattern = new RegExp("\\b.{1,"+truncateLength+"}\\b"+searchExpression+"\\b.{1,"+truncateLength+"}\\b","i");
    var res = sentence.match(pattern);
  console.log(pattern);
  return res;
}

var sentence = "As they rounded a bend in the path that ran beside the river, Lara recognized the silhouette of a fig tree atop a nearby hill. The weather was hot and the days were long. The fig tree was in full leaf, but not yet bearing fruit."+
"Soon Lara spotted other landmarks—an outcropping of limestone beside the path that had a silhouette like a man’s face, a marshy spot beside the river where the waterfowl were easily startled, a tall tree that looked like a man with his arms upraised";
var searchExpression = "tree";
var truncateLength = 20;


var result = truncate(sentence, searchExpression, truncateLength);

$(document).ready(function() {
    $(".result").html(result);
});

Upvotes: 2

Related Questions