Suresh Pattu
Suresh Pattu

Reputation: 6209

Remove all but the three word from a sentence and add three dots after that three sentence using jquery

I want to Remove all but the three word from a sentence and add three dots after that three sentence using jquery. example if the sentence is " We evaluate our borrowers on a periodic basis. See our evaluations " so in that remove "borrowers on a periodic basis. See our evaluations See our evaluations" and i want to show like this "We evaluate our..." i tried here is code

Upvotes: 0

Views: 643

Answers (4)

MiniduM
MiniduM

Reputation: 176

Try the following function:

function myfunction(val)
{
    temp = val.split(' ');

    if(val.length >= 2)
    {
         val = temp[0] + ' ' + temp[1] + ' ' + temp[2] + ' ..';
    }

    return val;
}

Upvotes: 0

Sandeep Rajoria
Sandeep Rajoria

Reputation: 1239

Javascript only supports lookaheads not lookbehinds...but yes there are work arounds

This will work for you...

var str="How are you doing today?";

regexp = new RegExp('(' + str.match(/(\w+\s){2}\w+/g)+ ')?.*'); //fetches first 3 words and makes regural expression to lookbehind this positively
var output = str.replace(regexp, function($0, $1){     //mimicks positive lookbehind
    return $1 ? $1 + '...' : $0;
});

document.writeln(output );

UPDATE - this worked for the "How are you.." only....so use the below one

Here is a very good tutorial to mimicking negative and positive lookbehinds in javascript as it does not support it...

Javascript only supports lookaheads

http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

UPDATE - this works fine for anything like in your example

var str="We evaluate our borrowers on a periodic basis. See our evaluationsWe evaluate our borrowers on a periodic basis. See our evaluationsWe evaluate our borrowers on a periodic basis. See our evaluations";

var first_three = str.match(/(\w+\s){2}\w+/);

regexp = new RegExp('(' + first_three[0] + ')?.*'); //fetches first 3 words and makes regural expression to lookbehind this positively
var output = str.replace(regexp, function($0, $1){     //mimicks positive lookbehind
    return $1 ? $1 + '...' : $0;
});

document.writeln(output);​

and a running jsFiddle http://jsfiddle.net/Rpq7b/2/

Upvotes: 2

Joberror
Joberror

Reputation: 5890

I guess this should work

 var yourText = " We evaluate our borrowers on a periodic basis", 

 result = yourText.replace(/^(.{15}[^\s]*).*/, "$1");

 alert(result);

Note: Play around with the 15 in the code to any suitable number of yours

To get the the ellipsis stuff: you can add result + "…"

Upvotes: 0

Josh Davenport-Smith
Josh Davenport-Smith

Reputation: 5511

You can use the css rule text-overflow to achieve this. Here's a class you can use to achieve the result you want:

.prevent-overflow { 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis
}​

Here's a break down of what this does: white-space: nowrap; stops the text form wrapping onto a new line when the edge of the element is reached. overflow:hidden tells the text to never appear outside the bounds of the element and text-overflow: ellipsis tells the browser to place ... at the end of the text whenever any gets cut off (see text-overflow @ MDN).

Apply it to an element with dimensions that will cut off the text contained within it.

I've created a jsfiddle to demonstrate it's use.

Note: The jsfiddle contained a div at the end of the element (<div style="clear:both"></div>) which stops the ellipsis from working (see this jsfiddle), I suggest you apply clear:both; to the entire div instead, or find another way to achieve whatever the purpose of that <div> was.

Upvotes: 2

Related Questions