0

I would like to split sentences from a paragraph using these delimiters: (Notice each punctuation mark has space after it to not split, for example floating numbers i.e. 2.567

var d = ['. ', '; ', ': ', '? ', '! '];

Once I'm done modifying these sentences, I would like to join (glue) these sentences and restore the punctuation as it was.

How can I do so?

Gregory R.
  • 1,815
  • 1
  • 20
  • 32
  • What was/is your approach and where/how did it fail? Did you hear already of character class syntax, of grouping and capturing? Or do you start from scratch? – Peter Seliger Apr 19 '20 at 18:00
  • 2
    [Here is a solution](https://stackoverflow.com/a/59664804/3832970), but you need to remember to build your regex dynamically and [escape the delimiters](https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript). – Wiktor Stribiżew Apr 19 '20 at 18:03
  • I don't mean to undermine the person that closed this question. I had an answer 90% complete before it was closed, so I re-opened it as not to waste the work. – Mulan Apr 19 '20 at 18:08

1 Answers1

-1

The idea here is we build a RegExp using your input d. In the end we want it to look like -

const reLiteral =
   /[.;:?!](?= )/

To make the matches appear in .split output, we add (...) around the entire pattern

const reLiteralWithCapture =
  /([.;:?!](?= ))/

However since we will be building the RegExp from an array of strings, we need to use the new RegExp(...) constructor -

const d = 
  [ ".", ";", ":", "?", "!" ]

const re =
  new RegExp(`([${d.join()}](?= ))`)

Here it is in a sample program -

const str =
  "Don't forget, you will have to pay $4.10! That's the going rate for a good apple nowadays; not a bad rate to be honest. What, you don't think so? We can agree to disagree. No? Fine we will disagree to agree then."

const d = 
  [ ".", ";", ":", "?", "!" ]

const re =
  new RegExp(`([${d.join()}](?= ))`)

const parts =
  str.split(re)

console.log(parts)

Whether that matches your exact needs is unknown to me. Hopefully it's enough to get you started and show you where you can make the necessary changes to finish the problem.

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 2
    Please re-close. You may add the answer in the [original thread](https://stackoverflow.com/questions/59664482/join-the-string-with-same-separator-used-to-split). – Wiktor Stribiżew Apr 19 '20 at 18:26
  • 2
    You are using `g` with a regex used in `.split()` - `g` is redundant there. `split` always splits at all matches, it is the default behavior. Also, you are not escaping chars inside the character class, so any `]` or ``\`` or `-` or `^` may ruin your solution. – Wiktor Stribiżew Apr 19 '20 at 18:31