JavaScript Reguexp Object
What is RegExp ?
RegExp Modifiers
test()
exec()
What is RegExp ?
A regular expression is an object that describes a pattern of characters.
When you search in a text, you can use a pattern to describe what you are searchig for.
A simple pattern can be a single character.
A more complicated pattern can consist of more characters and can be used for parsing,
format checking,
substitution, and more
Regular expressions are used to perform powerful pattern-matching and search and-replace function on text.
var txt=new RegExp(pattern,modifiers);
or
var txt=/pattern/modifiers;
1.The pattern specifies the pattrn of an expression
2.The modifiers specifys whether a serch should be globbal, case-sensitive, and so on.
RegExp Modifiers
Modifiers are used to perform case-insensitive and global searches.
The i modifier is used to perform case-insensitive matching.
The g modifier is used to perform a global match(find all matches rather than stopping after the first match).
case-insensitive search for "w3schools" in a string:
var str="Visit w3schools";
var patt1=/w3schools/i;
<html>
<body>
<script type="text/javascript">
var str="visit w3schools";
var patt1="/w3schools/i;
document.write(str.match(patt1));
</script>
</body>
</html>
global search for "is"
var str="Is this all there is?";
var patt1=/is/g;
<html>
<body>
<script type="text/javascript">
var str="Is this all there is ?";
var patt1=/is/g;
document.write(str.match(patt1));
</script>
</body>
</html>
how to do a global, case-insensitive search for "is"
var str="Is this all there is ?";
var patt1=/is/gi;
<html>
<body>
<script type="text/javascript">
var str="Is this all there is ?";
var patt1=/is/gi;
document.write(str.match(patt1));
</script>
</body>
</html>
test()
test() method searches a string for a specified value and returns true or false.
depending on the result.
var patt1=new RegExp("e");
document.write(patt1.test("The best thing in life are free"))
Because there is an "e" in the string,
output:true
<html>
<body>
<script type="text/javascript">
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
</script>
</body>
</html>
exec()
The exec() method searches a string for a specified value and returns the text of the found value.
If no match is found, it returns null.
searches a string for the character"e":
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
output:e
<html>
<body>
<script type="text/javascript">
var patt1=new RegExp("e");
documnet.write(patt1.exec("The best things in life are free:"));
</script>
</body>
</html>
What is RegExp ?
RegExp Modifiers
test()
exec()
What is RegExp ?
A regular expression is an object that describes a pattern of characters.
When you search in a text, you can use a pattern to describe what you are searchig for.
A simple pattern can be a single character.
A more complicated pattern can consist of more characters and can be used for parsing,
format checking,
substitution, and more
Regular expressions are used to perform powerful pattern-matching and search and-replace function on text.
var txt=new RegExp(pattern,modifiers);
or
var txt=/pattern/modifiers;
1.The pattern specifies the pattrn of an expression
2.The modifiers specifys whether a serch should be globbal, case-sensitive, and so on.
RegExp Modifiers
Modifiers are used to perform case-insensitive and global searches.
The i modifier is used to perform case-insensitive matching.
The g modifier is used to perform a global match(find all matches rather than stopping after the first match).
case-insensitive search for "w3schools" in a string:
var str="Visit w3schools";
var patt1=/w3schools/i;
<html>
<body>
<script type="text/javascript">
var str="visit w3schools";
var patt1="/w3schools/i;
document.write(str.match(patt1));
</script>
</body>
</html>
global search for "is"
var str="Is this all there is?";
var patt1=/is/g;
<html>
<body>
<script type="text/javascript">
var str="Is this all there is ?";
var patt1=/is/g;
document.write(str.match(patt1));
</script>
</body>
</html>
how to do a global, case-insensitive search for "is"
var str="Is this all there is ?";
var patt1=/is/gi;
<html>
<body>
<script type="text/javascript">
var str="Is this all there is ?";
var patt1=/is/gi;
document.write(str.match(patt1));
</script>
</body>
</html>
test()
test() method searches a string for a specified value and returns true or false.
depending on the result.
var patt1=new RegExp("e");
document.write(patt1.test("The best thing in life are free"))
Because there is an "e" in the string,
output:true
<html>
<body>
<script type="text/javascript">
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
</script>
</body>
</html>
exec()
The exec() method searches a string for a specified value and returns the text of the found value.
If no match is found, it returns null.
searches a string for the character"e":
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
output:e
<html>
<body>
<script type="text/javascript">
var patt1=new RegExp("e");
documnet.write(patt1.exec("The best things in life are free:"));
</script>
</body>
</html>
No comments:
Post a Comment