2

got a problem with two regular expressions in javascript. I need one expression that matches, when my string contains no a-zA-Z. Like 12345. Stuff like \W\S or [^a-zA-Z] is not working, cause it matches 123a etc. Getting crazy with that one.

The other expression is the matching of two identical letters like the 'ss' in password. (.)\1 is not working with javascript. In c# no problem. What am I doing wrong with that one?

Made a section in my web.config which defines password-rules and in my code (c#, asp.net 3.5) I am reading the rules to create javascript for the client.

Thanks for help

4
  • (.)\1 works for me to find two consecutive matching characters. .../(.)\1/.test("password"); // true ... /(.)\1/.test("pasword"); // false Commented Aug 21, 2014 at 17:15
  • @cookiemonster I stand corrected. Commented Aug 21, 2014 at 17:17
  • You might want to use /[^a-zA-Z]+/ Commented Aug 21, 2014 at 17:18
  • Any example for me? Does this one need new Regex or does it work with .match(...) for you? Ah, OK. With new Regex. Wanted to use match only. Seems I have to rewrite my serverside-part from match to new Regex. In c# I iterate over all rules and handle all the same. Any other way instead of using .test? Commented Aug 21, 2014 at 17:21

2 Answers 2

6

You need to use start and end anchors also like this to make sure your complete string doesn't match any letters:

/^[^a-zA-Z]+$/
Sign up to request clarification or add additional context in comments.

5 Comments

Ahhh,of course. I was close. My other rule is for empty string ^$.
Or just /[a-z]/i and negate the result.
@Sardoan: To match empty string just change + to * in the regex. So make it /^[^a-zA-Z]*$/
I need for every rule one expression. In my webconfig I also define the name of the resource-name. So empty string has an own message-text and no letter has an other message. Over 20 culters defined. It is for a system with over 440.000 logins per day.
Ok that's fine then keep it separate. Also (.)\1 should work for you.
1

The following regex will work to match consecutive identical characters -

(\w)\1{1,}

4 Comments

12a3567a matches with (\w)\1{1,} only 1234567aa should match
12a3567a does not match with (\w)\1{1,} ... I tested the regex here - regular-expressions.info/javascriptexample.html
I tested my regular expressions too online and with c#. They worked. But now they fail beeing rendered in javascript. Got my test-project open and I try everything that gets posted ;)
I solved my problem with switching from match to test. I think my main problem was using string for the regex, like ...match("..") instead of (/../). That was new to me. Thanks for the help :) (I used my (.)\1)

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.