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

javascript - Regular expression to match A, AB, ABC, but not AC. ("starts with") - Stack Overflow

programmeradmin4浏览0评论

I'm banging my head against a wall. I want a regex that matches: empty string, A, AB, and ABC, but not AC. I have this, which works:

/^(A|AB|ABC)?$/

But this is a simplification; in my app A, B, and C are actually long character classes, so I don't want to repeat them over and over. Maybe I'm just not looking at it the right way. I tried this:

/^((AB?)C?)?$/

But that still matches AC.

Is there a simpler way to do this, that could be extended to (say), ABCD, ABCDE, etc.?

Edit: By extend to ABCDE, I mean it would match: empty string, A, AB, ABC, ABCD, ABCDE. Basically, a "starts with" regex.

I'm banging my head against a wall. I want a regex that matches: empty string, A, AB, and ABC, but not AC. I have this, which works:

/^(A|AB|ABC)?$/

But this is a simplification; in my app A, B, and C are actually long character classes, so I don't want to repeat them over and over. Maybe I'm just not looking at it the right way. I tried this:

/^((AB?)C?)?$/

But that still matches AC.

Is there a simpler way to do this, that could be extended to (say), ABCD, ABCDE, etc.?

Edit: By extend to ABCDE, I mean it would match: empty string, A, AB, ABC, ABCD, ABCDE. Basically, a "starts with" regex.

Share Improve this question edited Jan 5, 2010 at 16:22 Jenni asked Jan 5, 2010 at 16:06 JenniJenni 1,7064 gold badges20 silver badges28 bronze badges 4
  • 1 In what way do you want to extend it? – Gumbo Commented Jan 5, 2010 at 16:09
  • C cannot be preceed by an A, or A must be followed by a B? – Rubens Farias Commented Jan 5, 2010 at 16:10
  • So you want to allow ABCD but not ACD and ABCDE but not ACDE? – Gumbo Commented Jan 5, 2010 at 16:11
  • Sorry, I clarified how I meant for it to be extended. It is kind of a "starts with" regex that I guess I'm really looking for. – Jenni Commented Jan 5, 2010 at 16:17
Add a comment  | 

4 Answers 4

Reset to default 15

Try this regular expression:

^(A(B(C)?)?)?$

I think you can see the pattern and expand it for ABCD and ABCDE like:

^(A(B(C(D)?)?)?)?$
^(A(B(C(D(E)?)?)?)?)?$

Now each part depends on the preceeding parts (B depends on A, C depends on B, etc.).

/^A(?:B(?:C)?)?$/

should do it.

This is using the non-capturing group construct (?: xxx ) so as not to mess up any match capturing you may be doing.

This should do it:

/^A(BC?)?$/

This seems a little extravagant, but it works for character classes as well as characters.

(You would always use indexOf if it could be expressed as a string.)

You used to be able to edit a RegExp, but now you need a new one with any change.

RegExp.prototype.extend= function(c){
 var s= '', rx= this.toString();
 rx= rx.replace(/(\W+)$/, c+'$1').replace(/^\/|\/$/g,'');
 if(this.global) s+= 'g';
 if(this.multiline) s+= 'm';
 if(this.ignoreCase) s+= 'i';
 return RegExp(rx, s);
}

String.prototype.longMatch= function(arr){
 // if(this=='') return true;
 var Rx= RegExp("^("+arr.shift()+")");
 var i= 0, L= Math.min(s.length, arr.length),
 M= this.match(Rx);
 while(i< L){
  if(!M) return false;
  Rx= Rx.extend(arr[i++]);
  M= this.match(Rx);
 }
 return M[0]==this;
}

var arr= ['A','B','C','D'];
var s= 'ABCD';// try various strings
alert(s.longMatch(arr));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论