• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 9. RegExp() > Example: Regular Expressions and Text Exercises

Example: Regular Expressions and Text Exercises

With a little practice using the regular expressions tools explained earlier in the chapter, you can achieve rather impressive results. The remaining examples demonstrate some rather complex string manipulation. Shakespeare, anyone?

Listing 9.26. Hamlet, JavaScript Style

<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var a = "((BB)||!(BB))==?"
var b = /\(B{2}\)/g
var c = /\|{2}/
var d = /!/
var e = /\(/
var f = /\)==\?/

alert(a) // returns "((BB)||!(BB))==?"
var m = a.replace(b, "to be")
var n = m.replace(c, " or ")
var o = n.replace(d, "not ")
var p = o.replace(e, "")
var q = p.replace(f, "\n that is the question")
alert(q)
// returns "to be or not to be\n that is the question"
//-->
</script>
</body>
</html>


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint