-1

I have an element with text like so:

<div class="element">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>

I want to target the second sentence in this text, Usually it would ends with . ? ! or line break.

function add_br(){

    //Assigning the element to a variable
    var element = document.getElementsByClassName('element')[0];

    //Find the 2nd sentence.
    ..

    //Add <br> after it
    ..
}

So how to target the 2nd sentence and add <br> after it?

Here is an example, Let's assume this is the text:

<div class="element">
    This is the first sentence. This is the second sentence! Is this the 3rd sentence? ..
</div>

The final result should be:

<div class="element">
    This is the first sentence. This is the second sentence! <br> Is this the 3rd sentence? ..
</div>
sc0ttj
  • 87
  • 11
User1804
  • 47
  • 6

1 Answers1

1

Use split with regex:

The split() method takes a delimeter as a param, we can make that multiple delimeters with regex.

Here’s a full list of special characters in JS Regex, which need to be escaped with a backslash: [ \ ^ $ . | ? * + ( ).

So we can use \., !, \?..

All together: /[\.!\?]+/

function append_br(){
    var result='';

    document.getElementsByClassName('element')[0]
      .innerHTML.split(/[\.!\?]/)
      .forEach(sentence => result += sentence.concat("<br>"));

    return result;
}

Based on these links:

EDIT: Tested, fixed and working.

sc0ttj
  • 87
  • 11
  • The function gets the 2nd sentence without the `. ? !` , But doesn't add `
    ` and leave the main text as it is
    – User1804 Aug 10 '19 at 23:32
  • The result is `Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged` – User1804 Aug 10 '19 at 23:36
  • instead of `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
    – User1804 Aug 10 '19 at 23:37
  • Updated to return the whole text. Still not tested. Uses ES6 arrow functions - older browsers might need it re-written in older JS (ES5). – sc0ttj Aug 11 '19 at 00:21
  • Both functions return `undefined` result is undefined – User1804 Aug 11 '19 at 00:45
  • fixed the function – sc0ttj Aug 11 '19 at 00:59
  • I will accept the answer, But there are 2 things: The `. ? !` are removed in the returned string and after each `. ? !` there is `
    `, This `
    ` should be added after the second `. ? !` not after all of them
    – User1804 Aug 11 '19 at 02:19