首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正则表达式从标题中删除文章- the,an,a

正则表达式从标题中删除文章- the,an,a
EN

Stack Overflow用户
提问于 2009-11-10 09:08:45
回答 5查看 3.5K关注 0票数 1

我需要一个正则表达式,将匹配歌曲标题的第一个字母没有像" the ","an","a“这样的文章。我正在为Mediatomb编写一个使用javascript的自定义导入脚本。我需要能够把歌曲放在字母顺序的文件夹中。

例如:"Panama.mp3“在文件夹"P”中,"The Gambler.mp3“在文件夹"G”中。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-11-10 09:38:36

多亏了上面的答案,这就是我想出来的。如果有任何方法可以改进,请告诉我。

代码语言:javascript
复制
(?:(the |a |an ))*(\S{1})(\S*)
票数 0
EN

Stack Overflow用户

发布于 2009-11-10 09:14:14

不确定你使用的正则表达式是什么味道,但是有:non-capture groups,你可以这样使用它:

代码语言:javascript
复制
(?:(the |a |an ))([a-zA-Z])

捕获第三组,它应该始终是第一个字母(不包括" the,a,an,...“。

编辑:意思是说捕获第一个字母的第二组。还要确保运行这个不区分大小写的命令。并获得一个好的正则表达式测试工具(我喜欢Expresso,但还有其他工具)。

Edit2:做了一些改进;) (?:(the|a|an) +)?([a-zA-Z0-9])

票数 3
EN

Stack Overflow用户

发布于 2017-01-26 03:29:15

Javascript示例-

代码语言:javascript
复制
const regex = /(?:(the|a|an) +)/g; 
const str = `the cat in the hat a hare `; 
const subst = ` `;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

非捕获组(?:(the|a|an) +)

第一捕获组(|a|an)

  • 第一个替代“”匹配字符字面上(区分大小写)
  1. 第二个替代“a”匹配字符字面上(区分大小写)
  2. 第三个替代" an“匹配字符字面上(区分大小写)

代码语言:javascript
复制
- matches the character   literally (case sensitive)
- Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
- g modifier: global. All matches (don't return after first match)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1705057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档