Search code examples
javascriptregexsentence

Javascript RegExp for splitting text into sentences with quotes and keeping the delimiter


I'm trying to split a sentence with .!? like it was done in this question, but also account for possible double quotes at the beginning and end of a sentence. I'm using this:

let str = '" Non. Es-tu sûr ? "';
let result = str.match(/[^\.!\?]+[\.!\?]+/g);

console.log(result)

But when I do it, the 2 characters after the ? are not caught. So instead of getting:

['" Non.', 'Es-tu sûr ? "']

I'm getting:

['" Non.', 'Es-tu sûr ?']

Is there anyway to split these sentences using regex?


Solution

  • Looks like all you need to do is optionally match "s at the beginning and end:

    let str = '" Non. Es-tu sûr ? "';
    console.log(
      str.match( /"?[^.!?]+[.!?]+(?: *")?/g )
    );