最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Splitting a string at question mark, exclamation mark, or period in javascript and retain those marks? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Dec 23, 2018 at 5:52 ey dee ey emey dee ey em 8,63315 gold badges72 silver badges128 bronze badges 1
  • Related: Javascript RegExp for splitting text into sentences and keeping the delimiter: stackoverflow./q/11761563/1066234 – Avatar Commented May 20, 2020 at 8:32
Add a ment  | 

3 Answers 3

Reset to default 8

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

const test = `abc?aaa.abcd?.aabbccc!`;
console.log(test.match(/[a-z]*[?!.]/g))

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

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论