RegExp to Parse and replace specific Words
- 我
Fwd: [ProQuest Alert] test fwd:TestFwd: test2fwd: fwd:test3 Fwd: fwd: 输入: - 我需要的是:
test fwd:TestFwd: test2fwd: fwd:test3 输出 - 我
test TestFwd: test2fwd: test3 输出:错误(解析)
我想删除的话,
解释:
这我的正则表达式:
1 | ?\b(Fwd:)|\[(.*?)\] ? |
演示:http:/ / / / / R regex101.com qltpwb 7
1 2 3 | var str ="Fwd: [ProQuest Alert] test fwd:TestFwd: test2fwd: fwd:test3 Fwd:"; str = str.replace(/ ?\b(Fwd:)|\[(.*?)\] ?/gi, ' '); console.log(str) |
这个正则表达式呢?
1 2 3 | var str ="Fwd: [ProQuest Alert] test fwd:TestFwd: test2fwd: fwd:test3 Fwd:"; str = str.replace(/\b(Fwd:)(\s|$)|\[(.*?)\]/gi, ' '); console.log(str) |
我只在regex101.com上尝试过。
一种解决方案是匹配不需要的内容并捕获要删除的内容:
1 | keep_me1|keep_me2|(delete_me) |
请参阅regex101.com上的演示。由于不支持lookbehinds和
1 2 3 4 5 6 7 8 9 10 11 | let data = 'Fwd: [ProQuest Alert] test fwd:TestFwd: test2fwd: fwd:test3 Fwd: fwd:'; let regex = /\w+:\w+|(\[[^\[\]]+\]|\bFwd:)/gi data = data.replace(regex, function(match, group1) { if (typeof(group1) =="undefined") { return match; } else { return ''; } }); console.log(data); |