Gaetan vdb
Gaetan vdb

Reputation: 23

Regex everything before three dots

I have a regex that splits a string (in javascript) after a dot (and some other punctuation marks) and takes everything before that. When there are 3 consecutive points, he splits this 3 times. I would like the regex to only split after the last consecutive point.

This is the current regex: /(?<=[.,?!])/

Now: Sentence one.| Sentence two.|.|.|

Desired result: Sentence one.| Sentence two...|

Example: https://regex101.com/r/Ld63r8/1

Upvotes: 0

Views: 403

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You may match the substrings you need using

 text.match(/[^.,?!]+[.,?!]*/g)

See the regex demo.

Pattern details

  • [^.,?!]+ - 1 or more chars other than a ., ,, ? and !
  • [.,?!]* - 0 or more ., ,, ? or ! chars.

JS demo:

var text = "This is match one. This is the second, and the third? I want this to be the fourth and last...";
console.log(text.match(/[^.,?!]+[.,?!]*/g));

Upvotes: 2

Related Questions