topical media & game development

talk show tell print

mobile-js-parts-ch07.txt / txt



  chapter: Regular Expressions
  ==================
  var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)
  (?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?/;
  
  var url = "http://www.ora.com:80/goodparts?q#fragment";
      
      
  ====================================
  var url = "http://www.ora.com:80/goodparts?q#fragment";
  
  var result = parse_url.exec(url);
  
  var names = ['url', 'scheme', 'slash', 'host', 'port',
      'path', 'query', 'hash'];
  
  var blanks = '       ';
  var i;
  
  for (i = 0; i < names.length; i += 1) {
      document.writeln(names[i] + ':' +
          blanks.substring(names[i].length), result[i]);
  }
      
      
  ====================================
  url:    http://www.ora.com:80/goodparts?q#fragment
  scheme: http
  slash:  //
  host:   www.ora.com
  port:   80
  path:   goodparts
  query:  q
  hash:   fragment
      
      
  ====================================
  ^
      
      
  ====================================
  (?:([A-Za-z]+):)?
      
      
  ====================================
  (\/{0,3})
      
      
  ====================================
  ([0-9.\-A-Za-z]+)
      
      
  ====================================
  (?::(\d+))?
      
      
  ====================================
  (?:\/([^?#]*))?
      
      
  ====================================
  (?:\?([^#]*))?
      
      
  ====================================
  (?:#(.*))?
      
      
  ====================================
  
      
      
  ====================================
  var parse_number = /^-?\d+(?:\.\d*)?(?:e[+\-]?\d+)?/i;
  
  var test = function (num) {
      document.writeln(parse_number.test(num));
  };
  
  test('1');                // true
  test('number');           // false
  test('98.6');             // true
  test('132.21.86.100');    // false
  test('123.45E-67');       // true
  test('123.45D-67');       // false
      
      
  ====================================
  /^   /i
      
      
  ====================================
  -?
      
      
  ====================================
  \d+
      
      
  ====================================
  (?:\.\d*)?
      
      
  ====================================
  (?:e[+\-]?\d+)?
      
      
  ====================================
  // Make a regular expression object that matches
  // a JavaScript string.
  
  var my_regexp = /"(?:\\.|[^\\\"])*"/g;
      
      
  ====================================
  // Make a regular expression object that matches
  // a JavaScript string.
  
  var my_regexp = new RegExp("\"(?:\\\\.|[^\\\\\\\"])*\"", 'g');
      
      
  ====================================
  function make_a_matcher(  ) {
      return /a/gi;
  }
  
  var x = make_a_matcher(  );
  var y = make_a_matcher(  );
  
  // Beware: x and y are the same object!
  
  x.lastIndex = 10;
  
  document.writeln(y.lastIndex);    // 10
      
      
  ====================================
  "into".match(/in|int/)
      
      
  ====================================
  \ / [ ] ( ) { } ? + * | . ^ 
      
      
  ====================================
  var doubled_words = /([A-Za-z\u00C0-\u1FFF\u2800-\uFFFD]+)\s+\1/gi;
      
      
  ====================================
  ! " #  % & ' ( ) * +, - . / :
  ; < = > ? @ [ \ ] ^ _ ` { | } ˜
      
      
  ====================================
  (?:!|"|#|$|%|&|'|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|@|\[|\\|]|^|_|` |\{|\||\}|˜)
      
      
  ====================================
  [!-\/:-@\[-`{-˜]
      
      
  ====================================
  / [ \ ] ^
      
      
  ==================


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.