Hello guys! welcome to my new tutorial post on JavaScript. In this tutorial, i will be making use of few examples to explain the RegExp property to you. Like i always do advice, make sure to ask your questions where necessary via the comment box below. Am going to be starting with the RegExp constructor property so make sure to read through very carefully.
The RegExp constructor Property
It returns a reference to the array function that created the instance's prototype.
Syntax
Its syntax is as follows -
RegExp.constructor
Return Value
Returns the function that created this object's instance
Example
Try the following example below.
<html>
<head>
<title> JavaScript RegExp constructor Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
document.write("re.constructor is : " + re.constructor );
</script>
</body>
</html>
<head>
<title> JavaScript RegExp constructor Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
document.write("re.constructor is : " + re.constructor );
</script>
</body>
</html>
Output
Below is the output of the above example.
re.constructor is : function RegExp( ) { [native code] }
You can also read our tutorial post on: Regular Expressions in JavaScript with examples
The RegExp global Property
The global property is a read-only boolean property of RegExp objects. It specifies whether a particular regular expression performs global matching, that is to know whether it was created with "g" attribute.
Syntax
Its syntax is as follows -
RegExpObject.global
Return Value
Returns "TRUE" if the "g" modifier is set, "FALSE" otherwise.
Example
Try the following example below.
<html>
<head>
<title> JavaScript RegExp global Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
if ( re.global ) {
document.write("Test 1 - Global property is set" );
} else {
document.write("Test 1 - Global property is not set");
}
re = new RegExp( "string" , "g" );
if ( re.global ) {
document.write("<br />Test 2 - Global property is set");
} else {
document.write("<br />Test 2 - Global property is not set");
}
</script>
</body>
</html>
<head>
<title> JavaScript RegExp global Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
if ( re.global ) {
document.write("Test 1 - Global property is set" );
} else {
document.write("Test 1 - Global property is not set");
}
re = new RegExp( "string" , "g" );
if ( re.global ) {
document.write("<br />Test 2 - Global property is set");
} else {
document.write("<br />Test 2 - Global property is not set");
}
</script>
</body>
</html>
Output
Below is the output of the above example.
Test 1 - Global property is not set
Test 2 - Global property is set
Test 2 - Global property is set
The RegExp ignoreCase Property
ignoreCase property is a read-only boolean property of RegExp objects. It specifies whether a particular regular expression performs a case-insensitive matching, that is to know whether it was created with "i" attribute.
Syntax
Its syntax is as follows -
RegExpObject.ignoreCase
Return Value
Returns "TRUE" if the "i" modifier is set, "FALSE" otherwise.
Example
Try the following example below.
<html>
<head>
<title> JavaScript RegExp ignoreCase Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
if ( re.ignoreCase ) {
document.write("Test 1 - ignoreCase property is set" );
} else {
document.write("Test 1 - ignoreCase property is not set");
}
re = new RegExp( "string" , "i" );
if ( re.ignoreCase ) {
document.write("<br />Test 2 - ignoreCase property is set");
} else {
document.write("<br />Test 2 - ignoreCase property is not set");
}
</script>
</body>
</html>
<head>
<title> JavaScript RegExp ignoreCase Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
if ( re.ignoreCase ) {
document.write("Test 1 - ignoreCase property is set" );
} else {
document.write("Test 1 - ignoreCase property is not set");
}
re = new RegExp( "string" , "i" );
if ( re.ignoreCase ) {
document.write("<br />Test 2 - ignoreCase property is set");
} else {
document.write("<br />Test 2 - ignoreCase property is not set");
}
</script>
</body>
</html>
Output
Below is the output of the above example.
Test 1 - ignoreCase property is not set
Test 2 - ignoreCase property is set
Test 2 - ignoreCase property is set
You can also read our tutorial post: JavaScript while Loop
The RegExp lastIndex Property
lastIndex is a read and write property of RegExp object. For the regular expressions with the "g" attribute set, it contains an integer that specifies the character position immidiately following the last match found by RegExp.exec() and the RegExp.test() methods. These methods use this property as the starting point for the next search they conduct.
This property allows you to call those methods repeatedly, to loop through all the matches in a string and works only if the "g" modifier is set.
This property is read and write, so you can set it at any time to specify where in the target string, the next search should be starting from. The RegEx exec() and test() automatically reset the lastIndex to 0 when they fail to find a match (or another match).
This property allows you to call those methods repeatedly, to loop through all the matches in a string and works only if the "g" modifier is set.
This property is read and write, so you can set it at any time to specify where in the target string, the next search should be starting from. The RegEx exec() and test() automatically reset the lastIndex to 0 when they fail to find a match (or another match).
Syntax
Its syntax is as follows -
RegExpObject.lastIndex
Return Value
Returns an integer that specifies the character position immidiately following the last match.
Example
Try the following example below.
<html>
<head>
<title> JavaScript RegExp lastIndex Property</title>
</head>
<body>
<script type = "text/javascript">
var str = "JavaScript is an interesting scripting language";
var re = new RegExp( "script" , "g" );
re.test( str );
document.write("Test 1 - Current Index: " + re.lastIndex );
re.test( str );
document.write("<br />Test 2 - Current Index: " + re.lastIndex );
</script>
</body>
</html>
<head>
<title> JavaScript RegExp lastIndex Property</title>
</head>
<body>
<script type = "text/javascript">
var str = "JavaScript is an interesting scripting language";
var re = new RegExp( "script" , "g" );
re.test( str );
document.write("Test 1 - Current Index: " + re.lastIndex );
re.test( str );
document.write("<br />Test 2 - Current Index: " + re.lastIndex );
</script>
</body>
</html>
Output
Below is the output of the above example.
Test 1 - Current Index: 10
Test 2 - Current Index: 35
Test 2 - Current Index: 35
The RegExp multiline Property
The multiline is a read-only boolean property of the RegExp objects. It specifies whether or not a particular JavaScript regular expression should perform multiline matching, that is to know whether it was created with "m" attribute.
Syntax
Its syntax is as follows -
RegExpObject.multiline
Return Value
Returns "TRUE" if the "m" modifier is set, "FALSE" otherwise.
Example
Try the following example below.
<html>
<head>
<title> JavaScript RegExp multiline Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
if ( re.multiline ) {
document.write("Test 1 - multiline property is set" );
} else {
document.write("Test 1 - multiline property is not set");
}
re = new RegExp( "string" , "m" );
if ( re.multiline ) {
document.write("<br />Test 2 - multiline property is set");
} else {
document.write("<br />Test 2 - multiline property is not set");
}
</script>
</body>
</html>
<head>
<title> JavaScript RegExp multiline Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp("string");
if ( re.multiline ) {
document.write("Test 1 - multiline property is set" );
} else {
document.write("Test 1 - multiline property is not set");
}
re = new RegExp( "string" , "m" );
if ( re.multiline ) {
document.write("<br />Test 2 - multiline property is set");
} else {
document.write("<br />Test 2 - multiline property is not set");
}
</script>
</body>
</html>
Output
Below is the output of the above example.
Test 1 - multiline property is not set
Test 2 - multiline property is set
Test 2 - multiline property is set
You can also read our tutorial post on: JavaScript For Loop
The RegExp source Property
source is a read-only string property of RegExp object. It contains the text of the RegExp pattern and this text does not include delimiting slashes used in regular expression literal, and it does not include the "g", "i", and "m" attributes.
Syntax
Its syntax is as follows -
RegExpObject.source
Return Value
Returns the text used for pattern matching.
Example
Try the following example below.
<html>
<head>
<title> JavaScript RegExp source Property</title>
</head>
<body>
<script type = "text/javascript">
var str = "JavaScript is an interesting scripting language";
var re = new RegExp( "script" , "g" );
ret.test( str );
document.write("The regular expression is: " + re.source );
</script>
</body>
</html>
<head>
<title> JavaScript RegExp source Property</title>
</head>
<body>
<script type = "text/javascript">
var str = "JavaScript is an interesting scripting language";
var re = new RegExp( "script" , "g" );
ret.test( str );
document.write("The regular expression is: " + re.source );
</script>
</body>
</html>
Output
Below is the output of the above example.
The regular expression is: script
Alright guys! we have come to the end of this tutorial on JavaScript RegExp properties. In my next tutorial post, i will be making use of some examples to illustrate the usage of the RegExp methods available in JavaScript.
Feel free to ask your questions where necessary and i will reply to them. Follow us on our social media platforms available in order to stay up to date with our latest tutorials.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and i will reply to them. Follow us on our social media platforms available in order to stay up to date with our latest tutorials.
Thanks for reading and bye for now.