Apa itu Regex?

Apa itu Regex?
Get Started > Details
  • Details
  • Install

Deskripsi.

Regex, atau regular expression, adalah urutan karakter yang digunakan untuk mencocokkan dan mencari pola teks dalam string. Regex memungkinkan Kamu untuk mencari, mengganti, atau memvalidasi teks berdasarkan pola tertentu. Ini adalah alat yang kuat untuk manipulasi dan analisis teks dalam pemrograman.

Ini beberapa kode Regex yang saya sering gunakan.

Dalam Format Tahun

  • 2001
  • 2002
  • 2003
  • 2004
  • Other
/^[0-9]{4}$/

Dalam Format Score

  • 1.1
  • 2.0
  • 9.9
  • 5.5
  • Other
/^\d+(\.\d{1,2})$/

Dalam Format Musim

  • Winter 2001
  • Spring 2001
  • Summer 2001
  • Fall 2001
  • Other
/^([Ww]inter|[Ss]pring|[Ss]ummer|[Ff]all) [0-9]{4}$/

Dalam Format Menit

  • 23 min
  • 20 min
  • 4 min
  • 19 min
  • Other
/^[0-9]{2} [Mm]in$/

Dalam Chapter

  • Chapter 01
  • Chapter 1.1
  • Chapter 2
  • Chapter 4.6
  • Other
/[cC]hapter\s(\d+(\.\d+)?)/

Comments

  1. I want to include both the chapter number and the text following it. From "Solo Leveling Chapter 0 Prologue" to "Chapter 0 Prologue" in the breadcrumb. <li class='i-block' itemprop='itemListElement' itemscope='itemscope' itemtype='http://schema.org/ListItem'>
    <a class='c-999' expr:href='data:post.url' itemprop='item'>
    <span itemprop='name'>
    <script type='text/javascript'>
    // Extract chapter information from the post title using regular expression
    var chapterRegex = /\bChapter (\d+\s*[^\n\r]*)/i;
    var match = chapterRegex.exec('<data:post.title/>');
    if (match) {
    document.write(match[1]);
    } else {
    document.write('<data:post.title/>');
    }
    </script>
    </span>
    </a>
    <meta content='3' itemprop='position'/>
    </li>

    ReplyDelete
    Replies
    1. maybe like this. <li class='i-block' itemprop='itemListElement' itemscope='itemscope' itemtype='http://schema.org/ListItem'>
      <a class='c-999' expr:href='data:post.url' itemprop='item'>
      <span itemprop='name' id='chapterName'></span>
      <script type='text/javascript'>
      // Extract chapter information from the post title using regular expression
      var name = '<data:post.title/>';
      var chapterRegex = /\bChapter\s(.*)/i;
      var match = chapterRegex.exec(name);
      var chapterName = match ? match[0] : name;

      // Update the content of the span element with the extracted chapter name using textContent
      var t_chapterName = document.getElementById('chapterName');
      t_chapterName&&(t_chapterName.textContent=chapterName);
      </script>
      </a>
      <meta content='3' itemprop='position'/>
      </li>

      Delete
    2. but you can adjust it to your needs

      Delete
    3. Thank you, but it's not working with a longer title. How can I fix it? The series title is long, for example, "Villainess Level 99: I may be the Hidden Boss but I'm not the Demon Lord Chapter 10." And it's not appearing.

      Delete
    4. on var name = '<data:post.title/>'; change (') to (") and it will become var name = "<data:post.title/>"; it might clash on I'm because there's a sign ('), maybe

      Delete
    5. Yes, that's the problem. Thank you so much.

      Delete