JavaScript Tutorial/Regular Expressions/RegExp Object
Содержание
- 1 RegExp()
- 2 RegExp,$* (1)
- 3 RegExp.$1,$2,..$9
- 4 RegExp.$& (2)
- 5 RegExp,$_ (3)
- 6 RegExp.$` (4)
- 7 RegExp.$" (5)
- 8 RegExp.$+ (6)
- 9 RegExp.compile()
- 10 RegExp.exec()
- 11 RegExp.global
- 12 RegExp.ignoreCase
- 13 RegExp.input
- 14 RegExp.lastIndex
- 15 RegExp.lastMatch
- 16 RegExp.lastParen
- 17 RegExp.leftContext
- 18 RegExp.multiline
- 19 RegExp.rightContext
- 20 RegExp.source
- 21 RegExp.test()
RegExp()
Syntax
var variable = new RegExp(pattern, flags)
The RegExp() object represents a regular expression that is used for pattern matching.
The creation of the object takes pattern and flags parameters.
The pattern is a valid regular expression. The flags are either or both g (global) and i (ignore case).
Properties and Methods of the RegExp() Object
Property/Method Description RegExp.$* Represents multiline RegExp.$& Represents lastmatch RegExp.$_ Represents input RegExp.$` Represents leftContext RegExp.$" Represents rightContext RegExp.$+ Represents lastParen RegExp.$1,$2,...$9 Represents substring of matches compile() Compiles a regular expression exec() Executes the search for a match in a specified string global _Specifies whether to check the expressions against all possible matches ignoreCase Whether case is ignored or not during a string search input String that is matched lastIndex _Specifies the index at which to start matching the next string. lastMatch Last matched characters lastParen The last parenthesized substring match leftContext The substring preceding the most recent match multiline Specifies whether to search on multiple lines rightContext The substring following the most recent match source The string pattern test() Tests for a string match
RegExp,$* (1)
The RegExp,$* property reflects a multiline string search.
This is a Boolean, read-only value that reflects whether or not strings should be searched across multiple lines.
This is the same as using the multiline property.
<html>
<body>
<script language="JavaScript">
<!--
function getinfo(){
var myPat = new RegExp("the", "i");
var str = document.form1.mytext.value;
myArray = myPat.exec(str);
alert("RegExp.$* is: " + RegExp.multiline);
}
-->
</script>
<form name="form1">
<textarea name="mytext" cols="60" rows="8" onChange="getinfo()">
Change me and click outside to see the result.
</textarea>
<br>
</form>
</body>
</html>
RegExp.$1,$2,..$9
The RegExp.$1,$2,..$9 property represents parenthesized substring matches.
<html>
<body>
<script language="JavaScript1.2">
<!--
function swap(){
re = /(\w+)\D(\w+)/;
str = document.form1.text1.value;
newstr=str.replace(re, "$2, $1");
document.form1.text2.value = newstr;
}
-->
</script>
<form name="form1">
Enter your 7 digit phone number in the form xxx-xxxx
<br><br><br>
Phone Number (7 digits):<input type="text" name="text1" size=10>
<br><br>
<input type="button" value="Swap" onClick="swap()">
<br><br><br>
Output: <input type="text" name="text2" size=10
</form>
</body>
</html>
RegExp.$& (2)
The RegExp.$& property represents the last matched characters.
This is the same as using the lastMatch property.
<html>
<body>
<script language="JavaScript">
<!--
var pat = new RegExp("test", "gi");
str = "This is a test 123435";
myArray = pat.exec(str);
document.write("Pattern found: " + myArray[0] + ". the last match expression is: " + RegExp.lastMatch);
-->
</script>
</body>
</html>
RegExp,$_ (3)
The RegExp,$_ property represents the input to which a string is matched.
This is the same as using the input property.
<html>
<body>
<script language="JavaScript">
<!--
function getinput(){
var myPat = new RegExp("the", "i");
var str = document.form1.mytext.value;
myArray = myPat.exec(str);
alert("The RegExp.input is: " + RegExp.input);
}
-->
</script>
<form name="form1">
<br><br>
Enter some Text and click outside:
<input type="text" name="mytext" size="40" onChange="getinput()">
<br>
</form>
</body>
</html>
RegExp.$` (4)
The RegExp.$` property represents the substring preceding the most recent pattern match.
This is the same as using the leftContext property.
<html>
<body>
<script language="JavaScript">
<!--
pat = /is*/g;
var str = "This is a test.";
myArray = pat.exec(str);
document.write("In the string: " + "<b>");
document.write("I know where the fish is tonight" + "</b><br><br>");
document.write("The RegExp.leftContext is: " + RegExp.leftContext);
-->
</script>
</body>
</html>
RegExp.$" (5)
The RegExp.$" property represents the substring following the most pattern match.
This is the same as using the rightContext property.
<html>
<body>
<script language="JavaScript">
<!--
pat = /is*/gi;
var str = "This is a test.";
myArray = pat.exec(str);
document.write("The RegExp.rightContext is: " + RegExp.rightContext);
-->
</script>
</body>
</html>
RegExp.$+ (6)
The RegExp.$+ property represents the last parenthesized substring pattern match.
This is the same as using the lastParen property.
<html>
<body>
<script language="JavaScript">
<!--
exp = new RegExp("(is)", "g");
str = "This is a test (is)!";
myArray = exp.exec(str);
document.write("The RegExp.lastParen is: " + "<b>"+ RegExp.lastParen + "</b>");
-->
</script>
</body>
</html>
RegExp.compile()
Syntax
regexp.rupile(pattern, flag)
RegExp.exec()
Syntax
regexp.exec(string)
RegExp.global
The global property specifies whether or not the g flag is used with the regular expression.
If so, a global pattern match will be performed.
<html>
<body>
<script language="JavaScript">
<!--
var myPat = new RegExp("is", "g");
var str = "This is a test";
myArray = myPat.exec(str);
document.write("The value of RegExp.global is: " + "<b>" + myPat.global + "</b>");
-->
</script>
</body>
</html>
RegExp.ignoreCase
The ignoreCase property is a flag that informs the user if case is to be ignored during pattern matching or not.
<html>
<body>
<script language="JavaScript">
<!--
var myPat = new RegExp("is", "i");
var str = "This is a test";
myArray = myPat.exec(str);
document.write("The value of RegExp.ignoreCase is: " + "<b>"+ myPat.ignoreCase + "</b>");
-->
</script>
</body>
</html>
RegExp.input
The input property represents the string on which the pattern matching is performed.
<html>
<body>
<script language="JavaScript1.2">
<!--
function getinput(){
var myPat = new RegExp("the", "i");
var str = document.form1.mytext.value;
myArray = myPat.exec(str);
alert("The RegExp.input is: " + RegExp.input);
}
-->
</script>
<form name="form1">
<br><br>
Enter some Text and click outside:
<input type="text" name="mytext" size="40" onChange="getinput()">
<br>
<br><br><br><br><br><br><inputtype=name=size=<br><br><inputtype=value=</form>
</body>
</html>
RegExp.lastIndex
The lastIndex property gets the index of where the next match begins.
<html>
<body>
<script language="JavaScript">
<!--
exp=/is*/g;
str = "This is just a sample sentence.";
myArray = exp.exec(str);
document.write("Found: " + myArray[0] +". Next match starts at index: " + exp.lastIndex);
-->
</script>
</body>
</html>
RegExp.lastMatch
The lastMatch property represents the last matched characters.
<html>
<body>
<script language="JavaScript">
<!--
var pat = new RegExp("test", "gi");
str = "This is a test";
myArray = pat.exec(str);
document.write("Pattern found: " + myArray[0] + ". the last match expression is: " + RegExp.lastMatch);
-->
</script>
</body>
</html>
RegExp.lastParen
The lastParen property represents the last parenthesized substring match.
It returns a string value for the last parenthesized substring.
<html>
<body>
<script language="JavaScript">
<!--
exp = new RegExp("(is)", "g");
str = "This is a test (is)";
myArray = exp.exec(str);
document.write("The RegExp.lastParen is: " + "<b>" + RegExp.lastParen + "</b>");
-->
</script>
</body>
</html>
RegExp.leftContext
The leftContext property represents the substring preceding the most recent pattern match.
<html>
<body>
<script language="JavaScript">
<!--
pat = /is*/g;
var str = "This is a test";
myArray = pat.exec(str);
document.write("In the string: " + "<b>" + "I know where the fish is tonight" + "</b><br><br>");
document.write("The RegExp.leftContext is: " + RegExp.leftContext);
-->
</script>
</body>
</html>
RegExp.multiline
The multiline property determines whether pattern matching should be performed across multiple lines.
<html>
<body>
<script language="JavaScript">
<!--
function getinfo(){
var myPat = new RegExp("the", "i");
var str = document.form1.mytext.value;
myArray = myPat.exec(str);
alert("RegExp.$* is: " + RegExp.multiline);
}
-->
</script>
<form name="form1">
<br><br>
<textarea name="mytext" cols="60" rows="8" onChange="getinfo()">
Change me and click outside
</textarea>
<br>
</form>
</body>
</html>
RegExp.rightContext
The rightContext property represents the substring following the most recent pattern match.
<html>
<body>
<script language="JavaScript">
<!--
pat = /is*/gi;
var str = "This is a test";
myArray = pat.exec(str);
document.write("In the string: " + "<b>" + "Eat Drink and be Merry" + "</b><br><br>");
document.write("The RegExp.rightContext is: " + RegExp.rightContext);
-->
</script>
</body>
</html>
RegExp.source
The source property represents the text being used for pattern matching.
<html>
<body>
<script language="JavaScript">
<!--
exp = new RegExp("is", "g");
str = "This is a test";
myArray = exp.exec(str);
document.write("The source is: " + "<b>" + exp.source + "</b>");
-->
</script>
</body>
</html>
RegExp.test()
The test() method tests for a pattern match in a string.
Returns Boolean value true or false.
<html>
<body>
<script language="JavaScript">
<!--
myExp = new RegExp("is", "g");
str = "This is a test";
if(myExp.test(str)){
document.write("The test found \"hope\" in the string: " + "<b>" + " I hope everything is going well" + "</b>");
}
-->
</script>
</body>
</html>