Regular expression help

I have a regular expression that compares a string.  It is as follows:
var string = new RegExp( ' [ ' + leters + ' ][ ' + letters + ' 0-9]+ '  );

This is used to check if a string starts with a letter and then letters or numbers.

how do I modify this so it can start with letters OR numbers, instead of just letters?

Thanks
Wanting2LearnManAsked:
Who is Participating?
 
HonorGodSoftware EngineerCommented:
zepar303

^[a-zA-Z0-9]+

Lets the string start with either letters, or digits.
If the string MUST start with a letter, they would use:

^[a-zA-Z][a-zA-Z0-9]+

However, this would allow "non-letters and digits to follow, so they probably really want:

^[a-zA-Z][a-zA-Z0-9]+$
0
 
HonorGodSoftware EngineerCommented:
Do you really want to allow one, or more space?

If not, use this
var string = new RegExp( '[' + leters + '0-9][' + letters + '0-9]+ '  )

Open in new window

0
 
HonorGodSoftware EngineerCommented:
The square brackets identify a class of characters that are allowed to match.

when you have something like: [a-zA-Z] this matches any single lower, or uppercase letter.

To add digits to this collection of characters, use: [a-zA-Z0-9]

Again, this matches a single character.

To match a multiple instances of this group, follow the group by '+' to indicate "1 or more", or by '*' to indicate "zero or more"

[a-zA-Z0-9]+    <== Means "1 or more letters and/or digits"
[a-zA-Z0-9]*    <== Means "zero or more letters and/or digits"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
HonorGodSoftware EngineerCommented:
Since your original express includes a space before the zero (i.e., ' 0-9'), this means that a space is also allowed.

Is this what you want?
0
 
Wanting2LearnManAuthor Commented:
Thanks, the space was a mistake.

If I just wanted numbers would it be as follows:

var string = new RegExp( '[0-9]+' );  ??

Thanks
0
 
Wanting2LearnManAuthor Commented:
well???
0
 
HonorGodSoftware EngineerCommented:
Correct
0
 
zepar303Commented:
[0-9]+ <=== Means "1 or more numbers"
So yes it works if you just want numbers


But it seems that you also need to check that the letters are in a beginning of a string
So if I read your question correct you need to change your regexp to:
^[a-zA-Z0-9]+

Then every string starting with lowercase or uppercase or number would create a hit
And strings that are empty would not create a hit at all


There is a lot of logic you can build in, so creating this regexp also depends on the rest of the program where this line is used.
0
 
HonorGodSoftware EngineerCommented:
I had to get something to eat... :-)
0
 
HonorGodSoftware EngineerCommented:
Something to note about this express is that it does not indicate where in the string that 1, or more, digits must exist.

So, this RegExp would match each and every one of  the following strings:

'0'
'a1'
'abc123xyz'
'3.14159'
'1x'

If you want the string to be only digits, you should use the special "meta-characters" that are used to indicate:

'^' == Beginning of string
'$' == End of string

So, if you want a RegExp that matches any unsigned integer value, it would be:

/^[0-9]$/

ok?
0
 
zepar303Commented:
HonourGod: Correct, that end of String is a good check to have
And if any main credit goes to this question it is to HonourGod

But it sounded like the must of letters in the beginning was the old way, and that the user "Wanting2LearnMan" now want that to change to allowing numbers OR letters in the beginning.

This would give the final code (learning to use the code snippet here, hehe):
var string = new RegExp('^[' + letters + '0-9]+$');

Open in new window

0
 
HonorGodSoftware EngineerCommented:
Thanks for the grade & points.

Good luck & have a great day.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.