gpt4 book ai didi

Javascript正则表达式连续两次避免相同的字符

转载 作者:行者123 更新时间:2023-11-28 11:16:39 24 4
gpt4 key购买 nike

我需要一个正则表达式来避免连续两次使用相同的字符(@是字符),但可以在多个位置使用它们。例如:

someword@someword is ok
someword@@someword is not ok
someword@someword@someword is ok too.

所以基本上这是我现有的正则表达式 /^([a-zA-Z0-9'\-\x80-\xff\*\+ ]+),([a-zA-Z0-9 '\-\x80-\xff\*\+\@ ]+)$/ 其中第一组是姓氏,第二组是名字。我在第一个名称组中引入了一个神奇的字符@,保存时我将用空格替换它。问题是我不能有连续的@符号。

最佳答案

查找任何重复的字符(重复一次):

/(.)\1/.test(string) // returns true if repeated characters are found

查找重复的@:

string.indexOf('@@') !== -1 // returns true if @@ is found

关于Javascript正则表达式连续两次避免相同的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com