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);