- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript RegExp source property
The JavaScript source property is used to get the text or string of the Regular Expression pattern. For example:
<!DOCTYPE html> <html> <body> <p id="res"></p> <script> let myString = "I live in St. Louis"; let myPattern = /is/gi; document.getElementById("res").innerHTML = myPattern.source; </script> </body> </html>
A regular expression is defined in this code by the forward slash (/), which is a way to describe a pattern of characters to match in a string. The variable "myPattern" is assigned the regular expression.
let myPattern = /is/gi;
The regular expression "/is/gi" is made up of two parts: "g" and "i." With the "g" option, the regular expression will match all occurrences of the pattern in the string, not just the first one. The "i" option indicates that the matching should be case-insensitive, which means that both uppercase and lowercase versions of the pattern should match.
The word "is" serves as the regular expression pattern. This means that any instance of the characters "is" in a string will be matched by the regular expression.
The "source" property of the regular expression object is accessed after the regular expression has been defined using the dot (.) notation. The text of the regular expression pattern is stored as a string in the "source" property.
document.getElementById("res").innerHTML = myPattern.source;
The resultant string, "is", is then assigned to the innerHTML property of an HTML element with the ID "res". Therefore, the text "is" will be displayed within the p tag with ID "res" when the code is executed.
JavaScript source Syntax
The syntax of the source property in JavaScript is:
RegExpObj.source
It returns a string representing the textual part of the specified regular expression pattern.
Advantages of the "source" property in JavaScript RegExp
- The source property allows the regular expression pattern to be accessed as a string, which can be useful in certain situations.
- One can clone a regular expression by using the source property. By populating the source property of a new RegExp object with the value of an existing RegExp object, a new regular expression object with the same pattern as the original object can be created.
- The source property is useful for debugging regular expressions because it provides a quick way to examine the pattern being matched.
Disadvantages of the "source" property in JavaScript RegExp
- The options that were used to create the regular expression are not returned by the source property; only the regular expression pattern is. For instance, the source property would not contain this information if the regular expression was created with the i (case-insensitive) option.
- Since the pattern is typically already known and can be included directly in the regular expression code, the source property is typically not very useful in regular expression programming.
- When a regular expression pattern is generated dynamically, such as from user input, the source property is most helpful. The regular expression pattern is accessible via the source property, but only in read-only mode. You must make a new regular expression object using the modified pattern if you want to change the pattern.
« Previous Tutorial CodesCracker.com »