开发者

AS3 Regular Expression Question

开发者 https://www.devze.com 2022-12-24 06:03 出处:网络
Can someone give me a regular expression that will verify if all the letters in the word \"cat\" were also in the word \"coating\" in the proper sequence?开发者_C百科 So for the word \"coating\", the

Can someone give me a regular expression that will verify if all the letters in the word "cat" were also in the word "coating" in the proper sequence?开发者_C百科 So for the word "coating", the RegEx will test true for "cat" but false for "act".


If you're testing just in one word,

c[a-z]*a[a-z]*t

If you're trying to find words in a string,

\b[a-z]*c[a-z]*a[a-z]*t[a-z]*\b

Bear in mind, I'm not familiar with the ActionScript regex flavor specifically, but I imagine that it supports \b and lazy operators correctly, since those are fairly basic. Also, these regexes assume case-insensitivity.


Sounds to me like you want to check out the Adobe Live Docs, in the "Programming Actionscript 3.0" section (also found in Flash CS3's help files). In the Using Regular Expressions section, it is stated that

Regular expressions can be simple. For example, suppose you wanted to confirm that a particular string matches "ABC," or wanted to replace every occurrence of "ABC" in a string with some other text. In that case, you could use the following regular expression, which defines the pattern consisting of the letters A, B, and C in sequence:

/ABC/

Note that the regular expression literal is delineated with the forward slash (/) character.

They also provide an example in a later chapter of that section:

var pattern:RegExp = /Class/;
var str = "Class";
var strTwo = "Classes";  //(I added this, not in the docs)
trace(pattern.test(str)); // output: true
trace(pattern.test(strTwo)); //output: false (I added this line to show you how it works)

The livedocs are a good, often [unfortunately] omitted, source of help for any programmer in Actionscript. You can even download them in PDF format!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号