JavaScript source Property

JavascriptWeb DevelopmentObject Oriented Programming

The JavaScript source property returns the regexp text against which a given pattern is to matched.

Following is the code for the source property −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 18px;
      font-weight: 500;
      color: red;
   .result {
      color: rebeccapurple;
<h1>JavaScript source property</h1>
<div class="sample">The king bought a ring</div>
<div class="result"></div>
<button class="Btn">CLICK HERE</button>
Click on the above button to get the regex source text
   let resultEle = document.querySelector(".result");
   let pattern = /ing/gi;
   document.querySelector(".Btn").addEventListener("click", () => {
      resultEle.innerHTML = "Regexp text = " + pattern.source;


On clicking the ‘CLICK HERE’ button −

Updated on 12-May-2020 05:52:30