在 javascript 中,我想提取单词列表以 'y' 结尾。
代码如下,
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
str.match(/(\w+)y\W/g);
Run Code Online (Sandbox Code Playgroud)
结果是一个数组
["simply ", "dummy ", "industry.", "industry'", "dummy ", "galley ", "only ", "essentially ", "recently "]
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是,我可以使用正则表达式获得没有“y”字符的单词列表吗?结果词表应该是这样的,
["simpl ", "dumm ", "industr.", "industr'", "dumm ", "galle ", "onl ", "essentiall", "recentl"]
Run Code Online (Sandbox Code Playgroud)
/(\w+)y\W/g 不起作用。
您需要所谓的先行断言:这(?=x)意味着此匹配项前面的字符必须匹配x,但不要捕获它们。
var trimmedWords = wordString.match(/\b\w+(?=y\b)/g);
Run Code Online (Sandbox Code Playgroud)