PDA

View Full Version : [Solved] RegEx question



michaelc
15 Sep 2009, 6:44 PM
I was hoping someone could give me a better patten.

I have this
Ext.form.VTypes["alfaEditVal"] = /^[a-zA-Z0-9][-_.a-zA-Z0-9]$/;

allow alfaNumeric and -_. if it is not the first position.
problem it's also required that the user can enter 1 character
( no one would, but that's the business rule )

how can I define it to allow "-" & "_" & "." and blank in all but the first position but also allow the user to enter only once character.

danh2000
15 Sep 2009, 7:07 PM
Curly braces allow you to specify occurance count:

_{1}

allows exactly 1 underscore

michaelc
15 Sep 2009, 8:59 PM
Curly braces allow you to specify occurance count:

_{1}

allows exactly 1 underscore

The number of occurrence is not the issue, it's that you can't put "-" & "_" & "." or blank in the first position but you can in the 2nd to the nth.

my expression did this, but it also did not let a field with just one character entered in,
So I want /^[a-zA-Z0-9][ -_.a-zA-Z0-9]$/ but that will allow only a single character to be entered as long as it's not "-" & "_" & "." or blank ;->

evant
15 Sep 2009, 9:18 PM
So why not:



var re = /^[a-zA-Z0-9]{1}[ -_.a-zA-Z0-9]*$/;

console.log(re.test('x'));
console.log(re.test('_'));
console.log(re.test('a_'));
console.log(re.test('_foo'));
console.log(re.test(''));
console.log(re.test('foo_bar'));

michaelc
1 Oct 2009, 10:53 AM
This seems to work
var re = /^[a-zA-Z][-_. a-zA-Z0-9]{0,255}$/;

the one you have failed with
console.log(re.test('foo#bar'));

Now I have one more question. please please. can't seem to find it.
I want another pattern for search that will allow * in the very last position only, be that the first position or the 7th

did a bit more research and testing and came up with this
var re = /^[a-zA-Z][-_. a-zA-Z0-9]{0,255}
*$|^
{1}$/; Search