例如:
str = "The quick the brown the fox the jumped the over the lazy the dog."
在这里,我想知道如何做以下两件事:
答案 0 :(得分:1)
正如OP要求的那样,代码如下:
"The"
计为"the"
。"the"
时剩余的额外空格。使用此正则表达式:
re = /((?:\bthe\b.*?){2})\bthe\b/
仅删除第三个"the"
:
str.sub(re, '\1')
# => "The quick the brown the fox jumped the over the lazy the dog."
删除每三分之一"the"
:
str.gsub(re, '\1')
# => "The quick the brown the fox jumped the over the lazy dog."