20. JavaScript RegExp Object

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>

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams