最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular expression preceding comma or ending comma - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 5

Well 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
发布评论

评论列表(0)

  1. 暂无评论