SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegExp.input, RegExp.leftContext, ...

    I found this in felgall's page.


    I added script tag
    Code:
    <script type="text/javascript">
    
    var re = /(t)he/g;
    var mystring = "Over the moon.";
    re.text(mystring);
    alert(RegExp.input); // or RegExp.$_
    alert(RegExp.leftContext); // or RegExp["$`"]
    alert(RegExp.rightContext); // or RegExp["$'"]
    alert(RegExp.lastMatch); // or RegExp["$&"]
    alert(RegExp.lastParen); // or RegExp["$+"]
    alert(re.source);
    
    </script>
    I don't see message box. Please tell me what I can do.
    Last edited by muazzez; Jan 19, 2007 at 10:17.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Typo. This:

    re.text(mystring)

    should be:

    re.test(mystring)

    Regular expressions have a test() method, which returns true or false if the regex matches something in the specified string.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well! Now it works.
    The code is here.
    I added alert, but it was not seen.
    Code:
      
    <script type="text/javascript">
    
    var re = /(\w)(\w+) (\w)(/w+)/ig;
    var myname = 'stePhEn cHaPMan';
    myname.match(re);
    myname = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase() + ' ' + RegExp.$3.toUpperCase() + RegExp.$4.toLowerCase();
    
    alert(myname) ;
    
    </script>

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var re = /(\w)(\w+) (\w)(/w+)/ig;
    var myname = 'stePhEn cHaPMan';
    myname.match(re);
    myname = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase() + ' ' + RegExp.$3.toUpperCase() + RegExp.$4.toLowerCase();
    lol. It doesn't do anyone much good if an article is posted with code that doesn't work.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •