1
votes

How can I select the text between 2 guidelines which are 2 words?

Example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer congue venenatis consectetur. Suspendisse in pretium elit, id euismod odio. Quisque sem lorem, laoreet et feugiat eget, elementum eu felis. Nunc quis nisi tellus. Vivamus pulvinar diam erat. Vestibulum rutrum pharetra eros, nec scelerisque nibh pellentesque vitae. Aliquam vitae justo elit. Sed dapibus interdum mollis. Nunc a volutpat quam. Curabitur malesuada tempor cursus.

I need to get as output all text from dolor to Curabitur by using these 2 words as markers.

3
Is this text in a textarea, or just in an element on the page? Select usually implies a textarea, but I wonder if you just mean you want to get the text between those words into a variable.Jamiec
Post the code you've tried.j08691
@Jamiec It is in a textarea for now, dont think to change it to a div, but i may need this later.Radu Dascălu
@j08691 i haven't tried any code because i'm pretty new to jquery.Radu Dascălu
@Jamiec i need to get this text in a variable to delete it.Radu Dascălu

3 Answers

1
votes
var p = $('p'),
    text = p.text();

console.log(
    text.substr(
        text.indexOf('dolor'),
        text.indexOf('Curabitur') -
            text.indexOf('dolor') +
            'Curabitur'.length
    )
);

DEMO

Quick explanation

First we get the index of the word dolor. That's the starting point.
After that, we get the index of the word Curabitur. Then we remove the index of the word dolor since we are indexing on the original text and not the cutted down by the word dolor.
Finally, if we want to get the word Curabitur aswell, just add the length of the word to the index.

Hope it helps you!

1
votes
var text = document.getElementById('textareaID').value;
var snippet = text.substring(text.indexOf('dolor'), text.indexOf('Curabitur'));
1
votes

javascript:

var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer congue venenatis consectetur. Suspendisse in pretium elit, id euismod odio. Quisque sem lorem, laoreet et feugiat eget, elementum eu felis. Nunc quis nisi tellus. Vivamus pulvinar diam erat. Vestibulum rutrum pharetra eros, nec scelerisque nibh pellentesque vitae. Aliquam vitae justo elit. Sed dapibus interdum mollis. Nunc a volutpat quam. Curabitur malesuada tempor cursus.';

//or: var text = $(element).text();
var snippet = text.substring(text.indexOf('dolor'), 
                             text.indexOf('Curabitur') + 'Curabitur'.length);
alert(snippet)