0

I need to separate each sentence from text into an array item using jQuery.

Let's say I have this text:

jQuery Fundamentals is designed to get you comfortable working through common problems you'll be called upon to solve using jQuery. Both versions 1.x and 2.x of jQuery support "current-1 versions" (meaning the current stable version of the browser and the version that preceded it) of Firefox, Chrome, Safari, and Opera.

I need to achieve this result:

Array (
    [0] = "jQuery Fundamentals is designed to get you comfortable working through common problems you'll be called upon to solve using jQuery."
    [1] = "Both versions 1.x and 2.x of jQuery support "current-1 versions" (meaning the current stable version of the browser and the version that preceded it) of Firefox, Chrome, Safari, and Opera."
)

Note that in the sentences there might be "." or other symbols, so I'm not sure if .split() will achieve the proper result.

I prefer to write code my own, so it will be great if answers suggest only the method achieving the result, or your thoughts of doing it.

Milen Radkov
  • 250
  • 1
  • 13
  • 1
    perhaps a regex...splitting on dots unless they come after a digit? – nem035 Aug 19 '16 at 07:01
  • 3
    Simple answer: You can't! A simple code will not know what is a sentence and what not. Split it by `.` and you would split it on the `1.x` and `2.x` too. Split it on `. ` (*space behind*) and you will possible not match split correct when space is missing. So basically it is not possible on a safty way ... – eisbehr Aug 19 '16 at 07:01
  • 4
    First of all you have to define what is a sentence. If you have managed that task you can go on :) – Marcus Aug 19 '16 at 07:04
  • To add to @nem035 and @eisbehr, do a split with `.` taking care of numbers and then trim white spaces. You should have sentences then. – Vivek Pradhan Aug 19 '16 at 07:04
  • Well, and then you write about an js object in one sentence, like `object.test`. Here you go ... @VivekPradhan – eisbehr Aug 19 '16 at 07:05
  • 2
    Example: "The fox jump over a car, a bus etc. and survived. That was awesome!" – Marcus Aug 19 '16 at 07:06
  • Thanks for the suggestions, but as @Glufu said, there might be a sentence that have ". ", so that won't work. – Milen Radkov Aug 19 '16 at 07:10
  • 1
    Possible duplicate of [Split string into sentences in javascript](http://stackoverflow.com/questions/18914629/split-string-into-sentences-in-javascript) – Alex Aug 19 '16 at 07:11
  • http://stackoverflow.com/questions/18914629/split-string-into-sentences-in-javascript – Alex Aug 19 '16 at 07:11

2 Answers2

0

Try this.!!!

 jQuery(document).ready(function($){
            var str = "jQuery Fundamentals is designed to get you comfortable working through common problems you'll be called upon to solve using jQuery. Both versions 1.x and 2.x of jQuery support 'current-1 versions' (meaning the current stable version of the browser and the version that preceded it) of Firefox, Chrome, Safari, and Opera.";
            var lines = str.split('. '); //dot with space(your case)
            jQuery.each(lines, function(){
                alert(this);
            });
        });
Mohan Raj
  • 447
  • 1
  • 5
  • 18
  • It was alredy pointed out in the comments that this don't work! Beside this, your code has some other design problems and is not really great code ... – eisbehr Aug 19 '16 at 07:19
0

Usually we do this type of work either by split() method or regular expression. If you know the type of text explicitly like having a set of pattern, then you can go with regular expression. But sometimes regular expressions are slower than some other basic methods.

In your case, a simple suggestion: you can go with split() by '. ' -> dot and a space separator to get next line. Further more, search the next line having first character as a capital one.

str.split(". "); //search for a dot along with a space.
Saif Sabuj
  • 42
  • 4