RegExp Properties

Next ❯Methods

  • turned_in_notProperties
    • flags
    • source
    • lastIndex
    • global
    • ignoreCase
    • multiline
    • sticky
    • unicode

Syntax - general

RegExPattern.PropertyNames
  • To run below examples, try it inside <script> tag, in a basic html file


flags

To get all the flags used in RegExp object

Syntax

RegExPattern.flags

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.flags);
// Output: ig

Comment : Return used flags in pattern ('ig' in above case)



source

To get the pattern text

Syntax

RegExPattern.source

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.source);
// Output: is

Comment : Return used pattern text ('is' in above case)



lastIndex

To get/set the index at which pattern starts the next match

Syntax - To get lastIndex

RegExPattern.lastIndex

Syntax - To set lastIndex

RegExPattern.lastIndex=indexNumber
  • Works only if the "g" or "y" flag is enabled
    Default, always starts from index 0 & at the end set to 0
    Used when you have to modify the index place
  • Note! You can only set lastIndex, when using "y" flag or exec() or test() method, otherwise it's of no use

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.lastIndex); // start Output: 0

pattern.lastIndex=4;      // set the index value to 4
//  means start match from 4th index

while(pattern.test(str)==true){
  alert(pattern.lastIndex);   // Output: 7
}

// after index 7 their is no match left
alert(pattern.lastIndex); // end Output: 0

Comment : Returns index at which pattern start the next match



global

To check pattern is using 'g' (global) flag or not

Syntax

RegExPattern.global

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.global);
// Output: true

Comment : check 'g' flag enabled or not ('true' in above case)



ignoreCase

To check pattern is using 'i' (ignoreCase) flag or not

Syntax

RegExPattern.ignoreCase

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.ignoreCase);
// Output: true

Comment : check 'i' flag enabled or not ('true' in above case)



multiline

To check pattern is using 'm' (multiline) flag or not

Syntax

RegExPattern.multiline

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.multiline);
// Output: false

Comment : check 'm' flag enabled or not ('false' in above case)



sticky

To check pattern is using 'y' (sticky) flag or not

Syntax

RegExPattern.sticky

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.sticky);
// Output: false

Comment : check 'y' flag enabled or not ('false' in above case)



unicode

To check pattern is using 'u' (unicode) flag or not

Syntax

RegExPattern.unicode

For example :

var str="This Is a book";
var pattern=/is/ig;
alert(pattern.unicode);
// Output: false

Comment : check 'u' flag enabled or not ('false' in above case)


  • Methods
❮ Prev Conditional
Next ❯Methods
TryOut Examples"Learn to Explore..!"

TryOut Editor

receipt