Using regular expressions in JavaScript, we can separate a paragraph into individual sentences

Hey there! I'm currently working on a project that requires extracting sentences from a paragraph using regex in JavaScript. Unfortunately, my attempts to achieve this have resulted in syntax errors as I have tried methods used in other programming languages. I want the regex to not consider titles (like Mr., Mrs., etc.) as the end of sentences and to be sensitive to strings like "Michael G." so it won't interpret them as sentence endings. Below is the snippet of code with the regex pattern I've been working with. Despite trying multiple approaches, I still haven't been successful in getting it to work. Any assistance on this matter would be greatly appreciated.

var sentences = $(this).text()
.replace(/(((?![.!?]['".”]?\s).)*[.!?]['".”]?)(\s|$)/g,
'<span class="sentence"> $1 </span> $3<div class="commentform">' + formdialog + '</div>');

The paragraph from which sentences need to be extracted:

“He is trying to give a push,” said a senior American official, who spoke on the condition of anonymity because of the delicacy of the matter, adding that the talks were bogging down “both because of short-term irritants and slowness at getting at fundamental issues.” Mr. Kerry was a tireless prod and a frequent presence in Jerusalem and Ramallah leading up to the resumption of talks in July, and he has recently met for hours at a time with Prime Minister Benjamin Netanyahu of Israel and the Palestinian Authority president, Mahmoud Abbas, in neutral sites like New York, London and Rome. Now, after a brief period of dealing with other problems, he is returning to the region to throw himself back into the process.

Reporting from Riyadh, Saudi Arabia, Michael R. Gordon contributed to this report.

Answer №1

Ah, I see you're not doing too bad, but the issue at hand arises when a period doesn't signal the end of a sentence, as seen in examples like Mr. Kerry and Michael R. Gordon. Finding a foolproof solution for this can be quite challenging.

Depending on the context of the paragraph you're analyzing, you could try prohibiting matches after single letters followed by a dot and common abbreviations with dots. However, even then, phrases like 'mr. Kerry spoke to the president.' and 'where do you think you're going, mr.' may still cause trouble.
It's important to weigh the consequences of false positives (incorrectly splitting sentences) and false negatives (failing to split where needed) and adjust your approach accordingly.

Keep in mind that this is a tough nut to crack.
Many text processors, such as OpenOffice and Word, attempt to identify sentence endings to automatically capitalize the next letter. Likewise, smartphone keyboards often make similar attempts. Yet, none have achieved perfect accuracy; instances like those mentioned tend to trip them up.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating an error during mongoose callback

Currently, I am in the process of developing a Node.js + Express application. The database I am working with is Mongo, and I have integrated Mongoose to establish a connection with this database. In my code, I am attempting to handle exceptions within a M ...

Is it possible to retrieve the values of #select1 and #select2 before initiating the AJAX request?

I am presented with the following snippet of HTML code: <select id="choice1"> <option value="0">Option 0</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3 ...

What is the best way to call the app.js function from an HTML page in an Express.js environment

Is there a way to trigger the function init() { // } located in app.js from an HTML page? <a href='javascript:init();'> Trigger init </a> The issue with the code above is that it searches for function init() only on the client side ...

What are the steps to develop an ElectronJS application that displays "Hello world!" in the console after a button click?

Can anyone offer a helping hand? From the depths of imagination to the realms never before conceived by humans. Check out this HTML snippet: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hell ...

Whenever I try to access my Node.js API from Heroku's live server, I encounter a CORS policy error

Whenever I try to access my Node.js API from the Angular app (running on a local setup) and host the API on Heroku's live server, I encounter the following error: CORS policy: No 'Access-Control-Allow-Origin'. Interestingly, when I host the ...

What could be causing the page to refresh every time a post or update is made using a mock REST API with JSON

I have been using Json-Server to mock API requests, but I'm facing an issue where the page reloads every time I fetch or update a post. I've tried searching for a solution, but haven't found one yet. Interestingly, even though I followed a ...

What is the best way to organize two separate arrays based on a single shared variable?

I have two separate arrays containing information. One array includes the title of each national park and other related data, while the second array consists of alerts from the entire NPS system. The common factor between these arrays is the parkCode. How ...

Changing the Month Label Format from Short (MMM) to Long (MMMM) in Angular Material Datepicker

I am looking to customize the month labels in Angular Material datepicker. By default, the month view displays in MMM format and I want to change it to MMMM using custom MatDateFormats. export const APP_DATE_FORMATS: MatDateFormats = { parse: { dat ...

Controlling Javascript events

Currently, I am working on implementing an in-place editor using jQuery. The functionality is such that when you click on the text you wish to edit, it will replace the content with an input field, specifically a select tag. Everything seems to be functio ...

Begin counting when a particular div element is visible on the screen

I have a plugins.init.js file that contains a try-catch block which runs on page load. I am looking for a way to execute this code only once when the div with the class counter-value comes into view. Is there a method to achieve this? try { const count ...

Controlling the visibility of components or elements in Angular through input modifications

Is there a more efficient way to handle button disabling and enabling based on email validation in Angular? I already have form controls set up, but want to make the process cleaner. The goal is to disable the "Get Started" button by default if the email a ...

Update Table Row Background Color in Separate Table by Clicking Button Using JQuery

Two tables are involved in this scenario - one that receives dynamically added rows, and another that stores the data to be included. The illustration above displays these tables. Upon clicking the Edit button, the information from the selected row in Tab ...

Issue with HTTP headers not being effective in $.AJAX request

Regardless of the method I attempt to use for setting headers in an AJAX call, and no matter which header I set, every time there is a header present, the AJAX call is aborted and does not go through. However, if I try to make the call without setting any ...

What are some ways to design unique custom data grid toolbar elements?

I am having trouble syncing my custom toolbar buttons with the imported material data grid toolbar components. I would like them to match in style, specifically applying the material styles to my custom components. However, I have not been able to find a w ...

React using Firebase: Receiving error "Cannot use import statement outside a module"

I've been following a helpful tutorial on setting up firebase authentication with React. However, I encountered some issues while trying to configure Firebase. I have created a firebase.js file as per the tutorial instructions: import firebase from &q ...

Converting string to JSON format by splitting it based on names

I recently manipulated a string containing the values "title:artist" by utilizing the str.split method : res = song.split(":"); The resulting output is as follows: ["Ruby","Kaiser Chiefs"] Now, I am curious about how to include the name in this array f ...

Identify and sort JSON objects based on keys with multiple values

My JSON file contains objects structured like this: [ { "name" : "something", "brand": "x", "category" : "cars" }, { "name" : "something2 ...

Having trouble with clearInterval in my Angular code

After all files have finished running, the array this.currentlyRunning is emptied and its length becomes zero. if(numberOfFiles === 0) { clearInterval(this.repeat); } I conducted a test using console.log and found that even though ...

What is the best way to prevent a fetch request from being initiated before the authentication token has been received

After successfully logging in, the data request fetch function needs to send the request with the saved token bearer. However, the data fetch is not waiting for the token and is receiving an Unauthorized access code. This is how my data request fetch func ...

How can we use Cypress to check if we are at the final slide in the presentation?

I am facing a challenge with multiple slideshow files that contain varying numbers of slides. Some have 10 slides, while others have 80 slides. My goal is to test every slide by using the forward and backward arrow buttons for navigation. Currently, I am c ...