I am not very good with regular expression.
I have a string like:
var bigString = 'abc,xyz,def';
I want to create a regular expression that it looks for either preceding mas or ma at the end.
e.g:
Valid expressions will be : abc, ,xyz, ,def
I will appreciate any kind of help.
I am not very good with regular expression.
I have a string like:
var bigString = 'abc,xyz,def';
I want to create a regular expression that it looks for either preceding mas or ma at the end.
e.g:
Valid expressions will be : abc, ,xyz, ,def
I will appreciate any kind of help.
Share Improve this question asked Apr 26, 2012 at 15:33 emphaticsunshineemphaticsunshine 3,7757 gold badges34 silver badges42 bronze badges 4- 1 Are those the only valid values? Is "abc" valid? – Ateş Göral Commented Apr 26, 2012 at 15:36
- Just alphabetical characters, or alpha-numerics? – David Thomas Commented Apr 26, 2012 at 15:37
- Another question is, WHY? Maybe the problem you're trying to solve doesn't require a regular expression. Can you tell what you're exactly trying to do? – Ateş Göral Commented Apr 26, 2012 at 15:41
-
1
"Valid expressions will be : abc, ,xyz, ,def" -
/(abc,)|(,xyz,)|(,def)/
QED. – Neil Commented Apr 26, 2012 at 15:44
4 Answers
Reset to default 5Well that regex would be:
/(?:,[A-Za-z]+)|(?:[A-Za-z],)/
/(,\w+)|(\w+,)/
This one will explicitly match where a ma is either at the beginning or the end of the string.
This should work : (UPDATED)
/(,[\w]+)|([\w]+,)/
If you are forcing a form, I would use this.
It validates strings 1,2 or 3 as one or more alpha chars with a ma before, after or both.
string1 = 'abc,'
string2 = ',xyz,'
string3 = ',def'
/^(?:[a-z]+,|,[a-z]+,?)$/i