Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Javascript

जावास्क्रिप्ट लास्टइंडेक्स प्रॉपर्टी

<घंटा/>

जावास्क्रिप्ट में लास्टइंडेक्स प्रॉपर्टी एक मैच होने पर इंडेक्स पोजीशन लौटाती है और अगला मैच उसी पोजीशन से फिर से शुरू होता है। lastIndex प्रॉपर्टी तभी काम करती है जब 'g' संशोधक सेट हो।

JavaScript में lastIndex प्रॉपर्टी के लिए कोड निम्नलिखित है -

उदाहरण

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .sample,
   .result {
      font-size: 18px;
      font-weight: 500;
      color: red;
   }
</style>
</head>
<body>
<h1>JavaScript lastIndex Property</h1>
<div class="sample">The king bought an expensive ring.</div>
<div style="font-weight: bold; color: black;" class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to find the index of 'ing' in the above string
</h3>
<script>
   let sampleEle = document.querySelector(".sample");
   let resultEle = document.querySelector(".result");
   let regex = new RegExp("ing", "g");
   regex.test(sampleEle.innerHTML);
   document.querySelector(".Btn").addEventListener("click", () => {
      resultEle.innerHTML +='"ing" found Current Index = ' + regex.lastIndex + "<br>";
      regex.test(sampleEle.innerHTML);
      resultEle.innerHTML += '"ing" found Current Index = ' + regex.lastIndex;
   });
</script>
</body>
</html>

आउटपुट

जावास्क्रिप्ट लास्टइंडेक्स प्रॉपर्टी

'यहां क्लिक करें' बटन पर क्लिक करने पर -

जावास्क्रिप्ट लास्टइंडेक्स प्रॉपर्टी


  1. जावास्क्रिप्ट इन्फिनिटी संपत्ति

    यदि फ़्लोटिंग-पॉइंट संख्या की ऊपरी सीमा पार हो जाती है तो जावास्क्रिप्ट इन्फिनिटी प्रॉपर्टी अनंतता प्रदर्शित करती है और - इन्फिनिटी यदि फ़्लोटिंग पॉइंट संख्या की निचली सीमा पार हो गई है। जावास्क्रिप्ट इन्फिनिटी प्रॉपर्टी के लिए कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang="en

  1. जावास्क्रिप्ट स्रोत संपत्ति

    जावास्क्रिप्ट स्रोत गुण रेगेक्सपी पाठ लौटाता है जिसके विरुद्ध किसी दिए गए पैटर्न का मिलान किया जाना है। स्रोत संपत्ति के लिए कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" con

  1. जावास्क्रिप्ट में लास्टइंडेक्स प्रॉपर्टी

    जावास्क्रिप्ट में lastIndex प्रॉपर्टी एक मैच होने पर इंडेक्स पोजीशन लौटाती है और फिर अगला मैच उसी पोजीशन से फिर से शुरू होता है। lastIndex प्रॉपर्टी तभी काम करती है जब g संशोधक सेट हो। JavaScript में lastIndex प्रॉपर्टी के लिए कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang="en&