?
Solved

Regular expression help

Posted on 2009-02-16
12
Medium Priority
?
214 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:Wanting2LearnMan
  • 8
  • 2
  • 2
12 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 23651132
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 23651163
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 23651170
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Wanting2LearnMan
ID: 23651211
Thanks, the space was a mistake.

If I just wanted numbers would it be as follows:

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

Thanks
0
 

Author Comment

by:Wanting2LearnMan
ID: 23651409
well???
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23651547
Correct
0
 
LVL 2

Assisted Solution

by:zepar303
zepar303 earned 400 total points
ID: 23651554
[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
 
LVL 41

Expert Comment

by:HonorGod
ID: 23651556
I had to get something to eat... :-)
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23651680
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
 
LVL 41

Accepted Solution

by:
HonorGod earned 1600 total points
ID: 23651973
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
 
LVL 2

Assisted Solution

by:zepar303
zepar303 earned 400 total points
ID: 23657128
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 23963520
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
Progress

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question