quantifiers في JScript

If you cannot specify the رقم of حرف/ حروف that comprise a مطابقة, عادي expressions دعم the concept of quantifiers. تمكنك هذه quantifiers على تحديد عدد المرات أحد مكونات معينة تعبير عادي يجب أن يكون الحصول على تطابق أن يكون صحيحاً.

What Quantifiers Mean

الحرف

الوصف

*

التلائمات the preceding حرف أو subexpression zero أو المزيد مرة/مرات. For مثال, zo* التلائمات z و zoo. * هو equivalent إلى {0,}.

+

التلائمات the preceding حرف أو subexpression واحد أو المزيد مرة/مرات. For مثال, zo+ التلائمات zo و zoo, but not z. + هو equivalent إلى {1,}.

?

التلائمات the preceding حرف أو subexpression zero أو واحد الوقت. For مثال, do(es)? التلائمات the do في do أو does. ? هو equivalent إلى {0,1}

{n}

n هو a nonnegative عدد صحيح. التلائمات exactly n مرة/مرات. For مثال, o{2} does not مطابقة the o في Bob but التلائمات the الثاني o's في food.

{n,}

n هو a nonnegative عدد صحيح. التلائمات على الأقل n مرة/مرات. For مثال, o{2,} does not مطابقة the o في Bob و التلائمات الجميع the o's في foooood. o{1,} هو equivalent إلى o+. o{0,} هو equivalent إلى o*.

{n,m}

m و n are nonnegative integers, الموقع n <= m. التلائمات على الأقل n و على الأكثر m مرة/مرات. For مثال, o{1,3} التلائمات the أول three o's في fooooood. o{0,1} هو equivalent إلى o?. ملاحظة that you cannot put a مسافة between the فاصلة و the أرقام.

Since فصل أرقام could بسهولة exceed nine في a قطر أيمن متوسط إدخال مستند, you need a way إلى مؤشر الثاني أو three digit فصل أرقام. Quantifiers give you that capability. The following عادي تعبير التلائمات فصل headings مع أي رقم of أرقام:

/Chapter [1-9][0-9]*/

Notice that the quantifier appears بعد the range تعبير. Therefore, it applies إلى the entire range تعبير that, في this حالة, specifies فقط أرقام من 0 through 9, inclusive.

+ quantifier هو لا تستخدم هنا لأن هناك لا بالضرورة تحتاج إلى أن تكون رقماً في الموضع الثاني أو اللاحقة. ؟ حرف also ليس used because it limits the فصل أرقام إلى فقط الثاني digits. You want إلى مطابقة على الأقل واحد digit following فصل و a مسافة حرف.

If you know that فصل أرقام are limited إلى فقط 99 chapters, you can استخدم the following تعبير إلى specify على الأقل واحد but not المزيد الثاني digits.

/Chapter [0-9]{1,2}/

The disadvantage of the above تعبير هو that a فصل رقم أكبر من 99 will still فقط مطابقة the أول الثاني أرقام. Another disadvantage هو that فصل 0 would مطابقة. Better expressions for matching فقط الثاني أرقام are the following:

/Chapter [1-9][0-9]?/

أو

/Chapter [1-9][0-9]{0,1}/

The *, +, و ? quantifiers are الجميع referred إلى كـ greedy because they مطابقة كـ much نص كـ possible. However, sometimes you just want a الأدنى مطابقة.

For مثال, you may be يتم الآن البحث an HTML مستند for an occurrence of a فصل عنوان enclosed في an H1 علامة. That نص appears في your مستند كـ:

<H1>Chapter 1 – Introduction to Regular Expressions</H1>

The following تعبير التلائمات everything من the فتح أصغر من الرمز (<) إلى the أكبر من الرمز (>) that closes the H1 علامة.

/<.*>/

If you فقط want إلى مطابقة the فتح H1 علامة, the following, non-greedy تعبير التلائمات فقط <H1>.

/<.*?>/

بواسطة placing the ? بعد a *, +, أو ? quantifier, the تعبير هو transformed من a greedy إلى a non-greedy, أو الأدنى, مطابقة.

راجع أيضًا:

موارد أخرى

مقدمة إلى تعابير عادية