Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regexp expression help! [code inside]

    Hi everyone, this is my first post, but not first javascript! I'm hoping the members here will be able to help me solve this.

    Basically a system that i'm building allows clients to copy columns from excel, paste into a textarea, and create an unordered list (within a div) from the pasted values.

    I've got it working but spliting the value string on linebreaks, then iterating through the resulting array. Unfortunately this doesn't scale well when the client pastes thousands of rows. So i'm trying to take the split/iterate approach out of the formula by doing a global regular expression replace.

    The regular expression should be able to match the first non whitespace character on a line up to the line break (\n), match this to $1 and then wrap $1 with list item tags.

    I've attached a zip containing an .html and .xls example of what i've been working on, javascript and sample data is also posted below.

    Data
    Code:
    123456
    123457
    123458
    123459
    123460
    123461
    123462
    123463
    123464
    123465
    Script
    Code:
    <SCRIPT LANGUAGE="JavaScript1.2">
    	function regExpTest(){
    		var s = document.getElementById('input').value;
    		var re = new RegExp('(*)\n', 'g');
    		newstr = s.replace(re, '<li>$1</li>');
    		alert(re.test(s));
    		alert(newstr);
    	}
    </SCRIPT>
    <body>
    <textarea name="input" rows="10" id="input"></textarea>
    <a href="#" onclick="regExpTest();">Test</a>
    Thanks, this has been irking me for too long! & Sorry for the long message.

    Thanks

    Matt
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    var re = /(\w+)\n?/gi;
    newstr = s.replace(re, '<li>$1</li>');
    Hope that helps!

    Happy coding!

  • #3
    New to the CF scene
    Join Date
    Dec 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the help, i really appreciate the reply!

    i was just coming back to post my final code, got it working thanks to ray326 at webdeveloper.com.

    i used the code he posted and finished the script below. its pretty slick, processing all 10000 values pasted from the excel spreadsheet takes less than 2 seconds!

    thanks for the help, the snippet i've been working on is below.

    Code:
    <SCRIPT LANGUAGE="JavaScript1.2">
    function regExpTest(){
    	var s = document.getElementById('input').value;
    	
    	// convert to csv
    	var csv_re = new RegExp('\r\n', 'g');
    	var csv_str = s.replace(csv_re,', ');
    	
    	// convert to list
    	var list_re = new RegExp(/(\w+)/gm); 
    	var list_str = '<ul id="customList">' + s.replace(list_re,'<li>$1</li>') + '</ul>';
    
    	// output to ui
    	document.getElementById('input').value = csv_str;
    	document.getElementById('list').innerHTML = list_str;
    	document.getElementById('count').innerHTML = document.getElementById('customList').childNodes.length;
    }
    </SCRIPT>
    <body>
    	<a href="#" onclick="regExpTest();">Test</a>
    	<br>
    	<textarea name="input" rows="10" id="input"></textarea>
    	<br>
    	Count: <span id="count"></span>
    	<br>
    	<div id="list"></div>
    </body>


  •  

    Posting Permissions

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