caret
للبحث عن الأنماط في بداية السلاسل. هناك أيضًا طريقة للبحث عن الأنماط في نهاية السلاسل. يمكنك البحث في نهاية السلاسل باستخدام حرف dollar sign
$
في نهاية التعليمة regex. دع theEnding = "هذه قصة لا تنتهي أبدا"؛
let storyRegex = / story $ /؛
storyRegex.test (theEnding)؛
// يعود صحيح
let noEnding = "في بعض الأحيان يجب أن تنتهي القصة" ؛
storyRegex.test (noEnding)؛
// إرجاع خاطئة
$
) لمطابقة السلسلة "caboose"
في نهاية سلسلة caboose
. "caboose"
باستخدام علامة الدولار $
anchor في تعبيرك المعتاد.
testString: 'assert(lastRegex.source == "caboose$", "You should search for "caboose"
with the dollar sign $
anchor in your regex.");'
- text: يجب ألا يستخدم تعبيرك المعتاد أي أعلام.
testString: 'assert(lastRegex.flags == "", "Your regex should not use any flags.");'
- text: يجب أن تتطابق مع "caboose"
في نهاية السلسلة "The last car on a train is the caboose"
testString: 'assert(lastRegex.test("The last car on a train is the caboose"), "You should match "caboose"
at the end of the string "The last car on a train is the caboose"
");'
```