2

I'm rather new to Regex, and i'm working on this particular statement:

The input can't be empty, and it cannot contain any letters ([a-zA-Z]).

I have these 2 statements now:

Not empty: (?=\\s*\\S).*$)
No letters: ^[^a-zA-Z]

I know these 2 work. However, i need to combine them in a single statement (for use with Javascript). I have tried literally everything i can find and can think of. Putting the statements between ()'s, between []'s, separating them with |, combining them with ()(), adding and removing ^ and *$, and every combination thereof. However, it always seems to either be not empty or no letters, never both.

Can anyone help me combine these 2 statements into 1?

Davy
  • 691
  • 1
  • 7
  • 18
  • 6
    `/^[^a-zA-Z]+$/` - the `+` ensures there's at least one character. – Niet the Dark Absol Jul 07 '14 at 11:51
  • 2
    ... or just `/^[a-z]+$/i` – raina77ow Jul 07 '14 at 11:53
  • Thanks Absol, that indeed did the trick. I can't believe i just spend hours trying to combine it when it was a simple '+'. Now that i'm typing another question about Regex: Why is it that people always post regex as /regexHere/, but when i use the code i have to remove the /'s? What's the point in typing it with a single /? – Davy Jul 07 '14 at 11:57
  • 1
    @Mortaza: Regular expressions are often written with forward slashes as *delimiters*. For example, `/re/` is used for the regular expression re. Several languages, including JavaScript, use the same delimiter notation. See [this answer](http://stackoverflow.com/a/9622110/1438393) for an example. Check [Wikipedia](http://en.wikipedia.org/wiki/Regular_expression#Delimiters) for a brief history of how this came into existence. – Amal Murali Jul 07 '14 at 12:06

1 Answers1

5

Try the following:

/^[^a-zA-Z]+$/

(note: Niet the Dark Absol commented exactly what I just posted here, before I answered, so if you want to I'll delete my answer so you can pick up the upvotes.)

Husman
  • 6,819
  • 9
  • 29
  • 47
  • This one works :) Absol posted it first in the comment part, but i'll accept your answer for future references. – Davy Jul 07 '14 at 11:59