1

Iam pretty new to regular expressions .I would like to prevent the user from entering in

String data using javascript function with the following condition .

Same sequence of characters cannot be repeated two or more times

  aabhi - is allowed 
 aabbcc -  is allowed

 dayday - not allowed
 abab - not allowed
 ababab - not allowed 
 aaaa - not allowed 

this applies to numbers too .Can someone help me with this ? Thanks in advance

5
  • Why dayday not allowed but aa allowed? Commented Dec 4, 2013 at 16:39
  • well thats the requirement ;) since its 'sequence' and not characters that they have mentioned in condition , i guess that single character is allowed two times ..thanks for asking out .. Commented Dec 4, 2013 at 16:43
  • but aaa is not allowed which is single character? Commented Dec 4, 2013 at 16:44
  • am sorry ..Typo error .edited the question now aaaa not allowed Commented Dec 4, 2013 at 16:48
  • ok np, posted an answer below. Commented Dec 4, 2013 at 16:49

1 Answer 1

3

Based on your inputs you can use this regex:

 /(\w{2,})(\1)/

Code:

re = /(\w{2,})(\1)/;

s = 'dayday';
if (re.test(s))
   console.log("invalid);
else
   console.log("valid);
Sign up to request clarification or add additional context in comments.

2 Comments

It worked ! you made my day:) .Being new , could you suggest me best place to know or have a trial with regular expressions ?thanks once again
You're most welcome. Best reference for regex is: regular-expressions.info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.