Javascript regexp - only if first character is not an asterisk
Asked Answered
D

3

14

I am using a javascript validator which will let me build custom validation based on regexp

From their website: regexp=^[A-Za-z]{1,20}$ allow up to 20 alphabetic characters.

This will return an error if the entered data in the input field is outside this scope.

What I need is the string that will trigger an error for the inputfield if the value has an asterix as the first character.

I can make it trigger the opposite (an error if the first character is NOT an asterix) with:

regexp=[\u002A]

Heeeeelp please :-D

Dugald answered 14/3, 2011 at 1:32 Comment(0)
B
33

How about:

^[^\*]

Which matches any input that does not start with an asterisk; judging from the example regex, any input which does not match the regex will be cause a validation error, so with the double negative you should get the behaviour you want :-)

Explanation of my regex:

  • The first ^ means "at the start of the string"
  • The [ ... ] construct is a character class, which matches a single character among the ones enclosed within the brackets
  • The ^ in the beginning of the character class means "negate the character class", i.e. match any character that's not one of the ones listed
  • The \* means a literal *; * has a special meaning in regular expressions, so I've escaped it with a backslash. As Rob has pointed out in the comments, it's not strictly necessary to escape (most) special characters within a character class
Burrus answered 14/3, 2011 at 1:33 Comment(8)
you don't need to escape the * inside []Sanity
@Rob: You're probably right. I've found that it depends on the platform, but I've never had any problems explicitly escaping using a backslashBurrus
fair enough, I was only being pedantic because you got your answer in first :PSanity
@Rob: FGIW syndrome, sorry ;-) In any case, it's good for others to know that escaping inside a character class is usually unnecessaryBurrus
Actually this will trigger an validation error if there is an asterix anywhere in the input value, not only if the first character is an asterix...Dugald
@Splynx: Are you sure you included the first ^ (which anchors the rest of the regex to the beginning of the string)? The regex should only ever match a non-empty input with the first character not being a *Burrus
My bad, it did in fact only check on the first character it seems - any ways it validates just like it should - thank you again!Dugald
@Splynx: No problem ;-) By the way, if you need it to pass an empty input as valid too, you can use: ^(?:[^\*]|$)Burrus
B
3

How about ^[^\*].+.

Broken down:

  • ^ = start of string.
  • [^\*] = any one character not the '*'.
  • .+ = any other character at least once.
Bullivant answered 14/3, 2011 at 1:35 Comment(1)
This is better answer IMO.Leis
Q
0

You can invert character class by using ^ after [

regexp=[^\u002A]

Queenstown answered 14/3, 2011 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.