javascript - Finding punctuation marks in text with string-methods
问题描述
how can I find out when a punctuation(?!;.) or "<" character comes in the string. I don’t want to use an array or compare any letter, but try to solve it with string methods. Something like that:
var text = corpus.substr(0, corpus.indexOf(".");
Ok, if I explicitly specify a character like a punct, it works fine. The problem with my parsing is that with a long text in a loop, I no longer know how a sentence ends, whether with question marks or exclamation points. I tried following, but it doesn’t work:
var text = corpus.substr(0, corpus.indexOf(corpus.search("."));
I want to loop through a long string and use every punctuation found to use it as the end-of-sentence character.
Do you know how can I solve my problem?
解决方案
Use a regular expression:
var text = corpus.split(/[(?!;.)<]/g);
推荐阅读
- javascript - 关于 Redux Saga 的基本问题:“yield call(...)”是否阻塞?
- ruby-on-rails - Dockerize Rails:你没有写权限
- deep-learning - PyTorch 教程在训练时期使用测试数据集
- unix - 如何转换 - DOUBLE LOW-9 QUOTATION MARK „ 使用 SED 命令
- swiftui - 我们如何向手表应用程序添加启动屏幕?
- c# - 即使抛出异常,Unity 单元测试也会成功
- angular - 无法使用 ng 命令以角度添加 ng-bootstrap
- angularjs - 在Angular中触发click事件后如何防止另一个事件侦听器(mouseout)
- django - 如何最小化 django 搜索查询?
- java - Kubernetes SharedInformer 使用和定义