طابق التعبير العادي بالسلسلة

1

لدي نص وأريد الحصول على جميع التكرارات في مصفوفة ، مثل:

[
    ['{{ $slot }}'],
    ['{{$example }}'],
    ['{{ $Product2}}'],
    ['{{$category1 }}']
]

جربت المثال التالي:

const text = "<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>"

let data = text.match('/{{\s*\$\w+\s*}}')

console.log(data)

كما ترون أحصل null كنتيجة.

أي اقتراحات لماذا؟

أقدر ردودك!

4 الاجابة

2
افضل جواب

في JavaScript ، تختلف حرفية regexp عن حرفية السلسلة ، لذا فإن حجة match يجب أن يكون /{{\s*\$\w+\s*}}/ و لا '{{\s*\$\w+\s*}}' أو '/{{\s*\$\w+\s*}}' . لاحظ أنه لا توجد اقتباسات حوله. لذا حاول:

const text = "<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>"
let data = /{{\s*\$\w+\s*}}/g

الذي يعطي:

>>> data
Array(4) [
    "{{ $slot }}",
    "{{ $example }}",
    "{{ $category1 }}",
    "{{ $Product2}}"
]

لاحظ أن أضفت g وضع علامة بعد الخط المائل النهائي لـ regexp للمباراة لإرجاع جميع سلاسل المطابقة وليس فقط السلسلة الأولى.

كما لاحظ آخرون ، من الجيد أيضًا الهروب من الأقواس المتعرجة ، وإلا فستواجه مشاكل عندما تحتوي على رقم ، لأن هذا له معنى خاص للتسجيلات.

:مؤلف
1
افضل جواب

ما عليك سوى استخدام حرف RegExp الحرفي و:

  • الهروب من $ عبر \$ لمطابقة حرفيا $ حرف ولا تؤكد نهاية موضع الخط في منتصف التعبير العادي عن طريق الصدفة
  • استخدم ال g علامة المطابقة العامة لمطابقة كل تكرار في السلسلة

المزيد عن الأعلام المتاحة وفئات الشخصيات


لذا فإن التعبير العادي هو:

/{{\s*\$\w+\s*}}/g

مثال عملي:

const text = '<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>'

let data = text.match(/{{\s*\$\w+\s*}}/g)

console.log(data)

أو

... عبر مُنشئ RegExp ، تأكد فقط من الهروب بشكل صحيح من فئات أحرف RegExp والحاضر بالفعل \ الشخصيات عبر الرائدة \ :

const text = '<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>'

let data = text.match(new RegExp('{{\\s*\\$\\w+\\s*}}', 'g'))

console.log(data)

:مؤلف
1
افضل جواب

يرجى تجربة ما يلي ( انظر regex101.com ):

const text = "<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>";

let regex = /\{\{\s*(\$[\w_]+)\s*\}\}/g;

let data = text.match(regex);
console.log(data)
:مؤلف
1
افضل جواب

حاول بدون الخط المائل الأول

"{{\ s * \ $ \ w + \ s *}}"

مثال regex101

المباراة 1

Full match 35-46 {{ $slot }}

المباراة 2

Full match 302-315 {{$example }}

المباراة 3

Full match 452-467 {{$category1 }}

المباراة 4

Full match 589-603 {{ $Product2}}

:مؤلف
فوق
قائمة طعام