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?

4
  • 6
    /^[^a-zA-Z]+$/ - the + ensures there's at least one character. Commented Jul 7, 2014 at 11:51
  • 2
    ... or just /^[a-z]+$/i Commented Jul 7, 2014 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 /? Commented Jul 7, 2014 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 for an example. Check Wikipedia for a brief history of how this came into existence. Commented Jul 7, 2014 at 12:06

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

This one works :) Absol posted it first in the comment part, but i'll accept your answer for future references.

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.