Search code examples
javascriptstringcharacterstring-lengthsentence

find a sentence with a specific length from inserted text using javascript


I'm working on a game in which I need to use a sentance with a specific length.

Is it possible to find and show, in any form (It could be console log or alert), every sentence(beginning with ". " and ending with ".") with a specific length, from an inserted text by using javascript?

I thought that I would simply insert some ebook that I have and find every sentence that has 32 characters including spaces, if anyone has an idea how to achieve that would be great :)


Solution

  • Take the stringified text, use the '.split' method on the string with the argument '.'

    You will now have an array of all 'sentences.' Now from there we can .filter() the sentences array to only include sentences that are of length 32:

      let text = 'My name is aaron. I like stuff.'
      text = text.split('.')
      let sentancesOf32Length = text.filter(text => text.length === 32)