برمجة تعبير عادي

يمكنك استخدام تعبيرات عادية في Jscriptإلى البحث عن النقوش في سلسلة، قم باستبدال نص واستخراج سلاسل فرعية.

يتم الآن البحث

التالية Jscriptمثال العثور على الجميع تواجدات الكلمة.

العبارة التي تنشئ تعبير عادي هو

var re = /\w+/g;

/\w+/يحدد نقش إلى تطابق واحد أو المزيد أي من حرف/ حروف التالية: A-Z و a-z، 0-9، وحرف تسطير سفلي. gتحدد يؤشر (@) الذي يتبع النمط بحث يجب العثور على الجميع تواجدات نقش بدلاً من فقط في اﻷول مرة الحدوث.

يمكنك أيضا استخدم التالية بديل Jscriptبناء الجملة.

var re = new RegExp("\\w+", "g");

استرداد كل تطابق exec أسلوبيحفظ يتم الآن البحث، ابتداءاً من الموضع من lastIndex، حتى خالي هو التي يتم إرجاعها.

function SearchGlobal()
{
    var src = "The quick brown fox jumps over the lazy dog.";

    // Create a regular expression pattern that has a global flag.
    var re = /\w+/g;

    var result;

    // Get the first match.
    result = re.exec(src);
    while (result != null)
    {
        print (result.index + "-" + result.lastIndex + "\t" + result[0]);

        // Get the next match.
        // Because the global flag is set, the search starts at the
        // position of lastIndex.
        result = re.exec(src);
    }

    // Output:
    //  0-3 The
    //  4-9 quick
    //  10-15 brown
    //  16-19 fox
    //  20-25 jumps
    //  26-30 over
    //  31-34 the
    //  35-39 lazy
    //  40-43 dog
}

يبحث المثال التالي عن أول فقط مطابقة. لأن العام ( g) يؤشر هو لم يتم تعيينها، يبدأ بحث من بداية سلسلة بحث.

function SearchNonGlobal()
{
    var src = "The quick brown fox jumps over the lazy dog.";

    // Create a regular expression that does not have
    // a global flag.
    var re = /\w+/;

    // Get the first match.
    // Because the global flag is not set, the search starts
    // from the beginning of the string.
    var result = re.exec(src);

    if (result == null)
        print ("not found");
    else
        {   
        print (result.index + "-" + result.lastIndex + "\t" + result[0]);
        }

    // Output:
    //  0-3 The
}

استبدال

في ما يلي مثال، يتم استبدال تواجدات "عن" مع "a". مثيل "" هو لا يستبدل، نظراً لوضع علامة i(تجاهل حالة) هو غير مضمنة في علامات تعبير عادي.

مثال يستخدم استبدال أسلوب.

function ReplaceGlobal()
{
    var src = "The batter hit the ball with the bat ";
    src += "and the fielder caught the ball with the glove.";

    // Replace "the" with "a".
    var re = /the/g;
    var result = src.replace(re, "a");

    print(result);

    // Output:
    //  The batter hit a ball with a bat and a fielder caught a ball with a glove.
}

استخراج سلاسل فرعية

وضع الأقواس في تعبير عادي نقش ينشئ submatch التي يمكن تخزينها لاستخدامها لاحقاً.

في المثال التالي، يتضمن النمط submatches الثلاثة. يتم عرض السلاسل submatch مع كل مطابقة.

يتم إرجاع صفيفة بواسطة exec أسلوب. يحتوي عنصر الصفيف صفراً على التطابق الكامل والعناصر من 1 إلى nيحتوي على submatches.

function SearchWithSubmatches()
{
    var result;

    var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!";

    // Create a regular expression to search for an e-mail address.
    // Include the global flag.
    // (More sophisticated RegExp patterns are available for
    // matching an e-mail address.)
    var re = /(\w+)@(\w+)\.(\w+)/g;

    // Get the first match.
    result = re.exec(src);
    while (result != null)
    {
        print ("e-mail address: " + result[0]);

        // Get the submatched parts of the address.
        print ("user name: " + result[1]);
        print ("host name: " + result[2]);
        print ("top-level domain: " + result[3]);
        print ("");

        // Get the next match.
        result = re.exec(src);
    }

    // Output:
    //  e-mail address: george@contoso.com
    //  user name: george
    //  host name: contoso
    //  top-level domain: com

    //  e-mail address: someone@example.com
    //  user name: someone
    //  host name: example
    //  top-level domain: com
}

Flags

في the Jscript عادي تعبير /abc/gim, the g specifies the عمومي يؤشر, the i specifies the تجاهل حالة يؤشر, و the m specifies the multiline يؤشر.

The following جدول shows the allowed علامات.

Jscript يؤشر

If يؤشر هو موجود

g

بحث الكل occurrences of the نقش في the searched سلسلة instead of just the أول occurrence.

i

البحث متحسس لحالة الأحرف.

m

^ التلائمات positions following a \n أو \r, و

$ التلائمات positions قبل \n أو \r.

Whether أو not the يؤشر هو موجود, ^ التلائمات the موضع at the يبدأ of the searched سلسلة, و $ التلائمات the موضع at the إنهاء of the searched سلسلة.

ميزات إضافى

تكون الميزات البرمجية الإضافى التالية متوفر.

الميزة

الوصف

ترجمة الأسلوب (JScript)

تجميع تعبير عادي في أحد تنسيقات داخلية لتنفيذ أسرع.

اختبار الأسلوب

اختبار ما إذا كان نمط تحدث في سلسلة searched.

أسلوب البحث

قم بإرجاع موضع التطابق أول.

راجع أيضًا:

المرجع

كائن تعبير عادي

المبادئ

تعبير عادي بناء الجملة

موارد أخرى

قم بإنشاء تعبير عادي