Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js]正则匹配 #7

Open
VaJoy opened this issue Dec 15, 2015 · 8 comments
Open

[js]正则匹配 #7

VaJoy opened this issue Dec 15, 2015 · 8 comments

Comments

@VaJoy
Copy link
Member

VaJoy commented Dec 15, 2015

需求

匹配重复5次以上的字母或数字,删除其剩下最后3位之前的部分

var reg = //TODO:完成正则
var ret = "abbbbbeeee12222222223".replace(reg, "");
console.log(ret);  // abbbeeee12223
@bailnl
Copy link

bailnl commented Dec 15, 2015

var reg = /([a-zA-Z0-9])\1+(?=\1{3})/g;//TODO:完成正则
var ret = "abbbbbeeee12222222223".replace(reg, "");
console.log(ret);  // abbbeeee12223

提示: \w 是有_

@VaJoy
Copy link
Member Author

VaJoy commented Dec 15, 2015

@bailnl 只匹配数字字母,不匹配其它符号,.肯定不行

@bailnl
Copy link

bailnl commented Dec 15, 2015

@VaJoy 我改。 已更新

@VaJoy
Copy link
Member Author

VaJoy commented Dec 15, 2015

@bailnl 👍good,那我换一种写法

var reg = /([a-zA-Z\d])((?=\1{4})(\1))+/g

@DudeYouth
Copy link

var reg=/([a-z0-9])\1+(?=\1{3})/ig;

@VaJoy
Copy link
Member Author

VaJoy commented Dec 15, 2015

@YoungPigs 不能用i哦,这样rrrRRRR反而被匹配到了

@bluesrocker
Copy link

var str = 'attttt  gggggggggggg 66666bbbb';
var reg = /([a-zA-Z\d])\1+(?=\1{3})/g;
reg.lastIndex = 0;
console.log( str.replace(reg, "") );
//attt  ggg 666bbbb

@Jiasm
Copy link
Member

Jiasm commented Apr 16, 2018

来一个新特性吧,命名捕获组的处理方法

let result = 'abbbbbeeee12222222223'.replace(/(?<tag>[^\W_])\k<tag>{2,}/g, '$<tag>$<tag>$<tag>')
console.log(result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants