1

I was a bit surprised, that actually no one had the exact same issue in javascript...

I tried several different solutions none of them parse the content correctly.

The closest one I tried : (I stole its regex query from a PHP solution)

const test = `abc?aaa.abcd?.aabbccc!`;
const sentencesList = test.split("/(\?|\.|!)/");

But result just going to be ["abc?aaa.abcd?.aabbccc!"]

What I want to get is ['abc?', 'aaa.', 'abcd?','.', 'aabbccc!'] I am so confused.. what exactly is wrong?

ey dee ey em
  • 7,991
  • 14
  • 65
  • 121
  • Related: Javascript RegExp for splitting text into sentences and keeping the delimiter: https://stackoverflow.com/q/11761563/1066234 – Avatar May 20 '20 at 08:32

3 Answers3

8

/[a-z]*[?!.]/g) will do what you want:

const test = `abc?aaa.abcd?.aabbccc!`;
console.log(test.match(/[a-z]*[?!.]/g))
ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
  • 1
    Maybe `/[^?!.]*[?!.]/g` would be slightly more robust, but otherwise great answer. – Patrick Roberts Dec 23 '18 at 06:00
  • @PatrickRoberts lol I was just going to mark your answer as best answer because this is a match statement instead of using in split, thank you so much for your answer (which you deleted... lol) that I realized for some reason to use @ic3b3rg answer properly I have to add `()` before and after `/../` strange... do you two know why the `()` need to be included for split? – ey dee ey em Dec 23 '18 at 06:04
  • @Ezeewei you want to use `match()` for this. `split()` is definitely not the right operation. – Patrick Roberts Dec 23 '18 at 06:04
  • @PatrickRoberts oh WOW! very very interesting! Did not know that's doing similar stuff.... thank you for the tip! – ey dee ey em Dec 23 '18 at 06:07
2

To help you out, what you write is not a regex. test.split("/(\?|\.|!)/"); is simply an 11 character string. A regex would be, for example, test.split(/(\?|\.|!)/);. This still would not be the regex you're looking for.

The problem with this regex is that it's looking for a ?, ., or ! character only, and capturing that lone character. What you want to do is find any number of characters, followed by one of those three characters.

Next, String.split does not accept regexes as arguments. You'll want to use a function that does accept them (such as String.match).

Putting this all together, you'll want to start out your regex with something like this: /.*?/. The dot means any character matches, the asterisk means 0 or more, and the questionmark means "non-greedy", or try to match as few characters as possible, while keeping a valid match.

To search for your three characters, you would follow this up with /[?!.]/ to indicate you want one of these three characters (so far we have /.*?[?!.]/). Lastly, you want to add the g flag so it searches for every instance, rather than only the first. /.*?[?!.]/g. Now we can use it in match:

const rawText = `abc?aaa.abcd?.aabbccc!`;
const matchedArray = rawText.match(/.*?[?!.]/g);
console.log(matchedArray);
Bronzdragon
  • 345
  • 3
  • 13
  • your answer is really really detailed. I really love that! Thank you and your regex actually can run with full sentences with multiple words, which is exactly what I was looking for. Thank you! – ey dee ey em Dec 28 '18 at 02:23
0

The following code works, I do not think we need pattern match. I take that back, I have been answering in Java.

final String S = "An sentence may end with period. Does it end any other way? Ofcourse!";
final String[] simpleSentences = S.split("[?!.]");
//now simpleSentences array has three elements in it.
Gayathri
  • 95
  • 1
  • 1
  • 11
  • Thanks for answering but can you also add an explanation on how your code solves the issue? Also check the [help center](https://stackoverflow.com/editing-help) for info on how to format code. – Tyler2P Aug 10 '21 at 11:48
  • @Tyler2P, I added a more detailed code snippet. Does that help? And I just realised I have been answering in Java. Apologies! :D – Gayathri Aug 10 '21 at 14:58