Skip to content
Advertisement

Strip everything after the last sentence [closed]

I have this string:

JavaScript

Using javascript regex, how can I strip everything that is after the last ., ?, ! (the end of the last sentence).

With the above example, it should result to:

JavaScript

Advertisement

Answer

You could try a regex replacement:

JavaScript

The replacement logic here says to:

JavaScript

Then we replace that final punctuation mark. We could, in theory, have used a lookbehind, but this isn’t supported on all versions of JavaScript (not at least on SO’s demo tool).

Advertisement