Javascript Regular Expression - 3 Methods and 5 Properties (Reference)
The javascript regular expression object is used to match text against a pattern. It returns a regular expression object which has 3 methods and 5 properties.
Regular Expression Methods:
Regular Expression Properties:
exec()
Tests for a match in a string. Returns the first match.
var str = "Fallinfo";
var patt = /[A-Z]/g;
var x = patt.exec(str);
// F
test()
Tests for a match in a string. Returns true or false.
var str = "Fallinfo";
var patt = /[A-Z]/g;
var x = patt.test(str);
// true
toString()
Returns the string value of the regular expression.
var str = "Fallinfo";
var patt = /[A-Z]/g;
var res = patt.toString();
// /[A-Z]/g
global
Checks whether the "g" modifier is set.
var str = "Fallinfo";
var patt = /[A-Z]/g;
var x = patt.global;
// true
ignoreCase
Checks whether the "i" modifier is set.
var str = "Fallinfo";
var patt = /[A-Z]/i;
var x = patt.ignoreCase;
// true
lastIndex
Specifies the index at which to start the next match. Only works if the "g" modifier is set.
var str = "Fallinfo Fallinfo Fallinfo";
var patt = /in/g;
while (patt.test(str) == true) {
console.log(patt.lastIndex);
}
// 6
// 15
// 24
multiline
Checks whether the "m" modifier is set.
var str = "Fallinfo";
var patt = /[A-Z]/m;
var x = patt.multiline;
// true
source
Returns the text of the RegExp pattern.
var str = "Fallinfo";
var patt = /[A-Z]/g;
var x = patt.source;
// [A-Z]
