#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    JavaScript RegExp object problems


    Hello everyone,

    I have a homework I need to present in about a week. The criteria are:

    Write a web page with two text areas T1 and T2. The user enters a string into the text area T1 to present a regular expression. Then the user enters some text into the text area T2. Your code should output the text of T2 with highlighted elements corresponding to the matches for the regular expression of T1.

    P.S. There is an extra condition to match only the last occurence.

    So far I have generated this piece of code :
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function match()
    {
    	var testchar = new RegExp(document.m.value);
    	
    	if 		(document.n.value.match(testchar)) 
    			{
    				alert("Successful match"); 
    			}
    	
    	else	{ 
    				alert("No match"); 
    			}	
    			
    	
    }
    </script>
    
    
    
    
    </head>
    <body>
    <p><b> RegExp : </b></p> <textarea id="m" name="T1" cols="30" rows="5" >\bt[a-z]+\b</textarea>
    
    <p><b> Subject string : </b></p> <textarea id="n" name="T2" cols="30" rows="5" >This is a test of the JavaScript RegExp object</textarea>
    
    <form id="demomatch">
    <input type=submit value="Test Match" onClick="match()">
    </form>
    
    </body>
    </html>
    The problem is that when i click the "Test Match" button nothing happens (and i know that with the given Regular expression it should find a match with the string). I suspect that the function isn't constructing the RegExp object at all. There is probably some issues with the way I address the content of the two textareas. But I cannot find my error. Can someone help? I would really appreciate it.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Ask your professor or your TA. Me doing this for you isn't going to help you learn. You (or your parents) paid good money for these people to teach you this stuff.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    3
    Rep Power
    0
    I have probably not presented this the right way. I dont want you to do my homework for me , I just wanted a tip on how to reference the value of the textarea. I could replace them with forms (which i know how to reference) but i would get a lower mark for not using textareas.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Oh I see. Try document.getElementById('m').innerHTML or document.getElementById('m').value
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    3
    Rep Power
    0
    Thank you kind sir. You have made my life a whole lot brighter.

IMN logo majestic logo threadwatch logo seochat tools logo