javascript - Extracting words from a space (comma) separated string
问题描述
I am trying to write a regex that extracts words separated by spaces (optionally comma + space), removing the 'stack' prefixes from the words (if any). I am trying to find a pure regex solution without any post-processing of results or similarly (if possible). Please see the attempt bellow:
Input:
var x = "stackoverflow aa bbb, ccc"
Regex:
var rx = /((?:\s)?(?:stack)?(\w+))+/
Expected output:
var match = x.match(rx);
["stackoverflow aa bbb ccc", "overflow", "aa", "bbb", "ccc"]
Actual output:
["stackoverflow aa bbb ccc", " ccc", "ccc"]
解决方案
One way to get the same aforementioned results from a match()
output is using a positive lookbehind. But lookbehinds had not been existed in JavaScript until ECMA2018 and as I'm aware Google Chrome is the only browser that implemented this feature into their JavaScript engine (V8).
How this this achievable? We need two paths to match the words: one should match sub-strings that come after stack
and the other should match all words but ensures that they do not start with stack
:
/(?<=\bstack)\w+|\b(?!stack)\w+/
If spaces and commas are mandatory, take them into consideration:
/(?:(?<=\bstack)\w+|\b(?!stack)\w+)(?=[, ]|$)/
JS code:
var str = "stackoverflow aa bbb, ccc"
console.log(str.match(/(?:(?<=\bstack)\w+|\b(?!stack)\w+)(?=[, ]|$)/g))
Another way would be splitting on undesired parts but needs more clarification on the current requirement as it may contain more than just words right now:
var str = "stackoverflow aa bbb, ccc"
console.log(str.split(/\bstack|[, ]+/))
推荐阅读
- python-3.x - 更快的方法:在熊猫数据框中分配 vs 附加
- microsoft-graph-api - 如何使用 MS Graph API 从存档邮箱中获取消息
- excel - 使用 VBA 遍历两个不同的目录
- sql - 查询已被取消,因为此查询的估计成本 (15) 超过了配置的阈值 12。请联系系统管理员
- excel - Excel VBA - 将表格中除 x 行之外的所有行复制到另一个表格的底部
- excel - VBA,将变量写入单元格时字体大小发生变化
- php - 将内容添加到列而不覆盖的 PHP 表单
- python - 如何使用 pandas/python 将变量值放在列表的另一个列表中
- python - django迁移创建超级用户
- javascript - ExtJs 6.2 无法在被动事件侦听器中阻止默认值