Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » javascript regex generator
Closed Thread
Old 02-15-2009, 01:03 PM   #1 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default javascript regex generator

i prolly shouldn't use this at all. I'm unfamilar with RegEx. It could be funto download the javascript and learn how it works.

My understanding is that RegEx isused for string matching. So if I'm looking for a certain string in a long list of strings, I should be able to implement an algoritms with RegEx to find matches. This javascript tool should help me build RegEx code much faster....than learning it the right...errr...long way, lol.

JavaScript Lab - Tools - JS Regex Generator
office politics is offline  
Old 02-15-2009, 05:19 PM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: javascript regex generator

Regex sucks, lol.

I have a book from O'Reilly, Mastering Regular Expressions. I haven't read it much, though I should...

You likely won't be able to take apart working code and "figure it out" rather than just copying it. You'll at least have to learn what all the modifiers and junk are and do, which really, is the hard part.
__________________

Need website help? PM me!
CrazeD is offline  
Old 02-15-2009, 06:42 PM   #3 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: javascript regex generator

Quote:
Originally Posted by CrazeD View Post
Regex sucks, lol.

You likely won't be able to take apart working code and "figure it out" rather than just copying it. You'll at least have to learn what all the modifiers and junk are and do, which really, is the hard part.
not that i can't read and understand code,but more too tedious of a project. 1000+ lines. interesting stuff going on tho.

used this to format it for me Mobilefish.com - Javascript formatter



so crazed, what do you use to match strings? last night did the ol' increment + 1 through a whole file and told myself there has to be a better & faster way.



true i guess about the copy and paste though. here's some code i used for parsing different directory listings in a recent project.

Adarsh's blog : Sample code for parsing FtpwebRequest response for ListDirectoryDetails
office politics is offline  
Old 02-15-2009, 07:06 PM   #4 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: javascript regex generator

Well, I've never made anything that iterates through a file like that.

The most I've done with Regex really is make/modify a small bbcode parsing function in PHP.

Code:
function bbcode ($string, $imgtag) {
		$string = preg_replace("#\[ quote=(.*?)\]#si", "[ quote]<b>\\1 wrote:</b><br/>",$string); 
		
		//prepare: how often start- and end-tag occurrs
		$starttags = substr_count($string, '[ quote]');
		$endtags = substr_count($string, '[ /quote]');
		
		$overflow=abs($starttags-$endtags);
		
		for($i=0;$i<$overflow;$i++) {
			if($starttags>$endtags) $string=$string.'[ /quote]';
			elseif($endtags>$starttags) $string='[ quote]'.$string;
		}
			
		$string = preg_replace("#\[ quote\]#si", '<table width="95%" border="1" cellpadding="2" cellspacing="1" align="center"><tr><td class="gen">', $string, 10);
		$string = preg_replace("#\[ /quote\]#si", '</td></tr></table>',$string, 10); 
	  
		//remove overflowed quote-tags
	  
		$string=eregi_replace('\[ quote\]','',$string);
		$string=eregi_replace('\[ /quote\]','',$string);

		if ($imgtag == 1) {
			$string = preg_replace("#\[img align=(right)\]www.(.*?)\[/img\]#si", "<img src=\"http://\\2\" border=\"0\" class=\"floatRight\" />", $string);
			$string = preg_replace("#\[img align=(right)\](.*?)\[/img\]#si", "<img src=\"\\2\" border=\"0\" class=\"floatRight\" />", $string);
			$string = preg_replace("#\[img align=(left)\]www.(.*?)\[/img\]#si", "<img src=\"http://\\2\" border=\"0\" class=\"floatLeft\" />", $string);
			$string = preg_replace("#\[img align=(left)\](.*?)\[/img\]#si", "<img src=\"\\2\" border=\"0\" class=\"floatLeft\" />", $string);
			$string = preg_replace("#\[img\]www.(.*?)\[/img\]#si", "<img src=\"http://\\1\" border=\"0\" />", $string);
			$string = preg_replace("#\[img\](.*?)\[/img\]#si", "<img src=\"\\1\" border=\"0\" />", $string);
			$string = preg_replace("#\[img:(.*?)\](.*?)\[/img:(.*?)\]#si", "<img src=\"\\2\" border=\"0\" />", $string);
			$string = preg_replace("#\[url\]http://(.*?)\[img\]http://(.*?)\[/img\]\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>", $string);
			$string = preg_replace("#\[url\]www.(.*?)\[img\]www.(.*?)\[/img\]\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\"><img src=\"http://www.\\2\" border=\"0\"></a>", $string);    
			$string = preg_replace("#\[url=http://(.*?)\]\[img\]http://(.*?)\[/img\]\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>", $string);
			$string = preg_replace("#\[url=www.(.*?)\]\[img\]www.(.*?)\[/img\]\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>", $string);		
		} else {
			$string = preg_replace("#\[img\](.*?)\[/img\]#si", "\\1", $string);
			$string = preg_replace("#\[img:(.*?)\](.*?)\[/img:(.*?)\]#si", "\\2", $string);
			$string = preg_replace("#\[url\]http://(.*?)\[img\]http://(.*?)\[/img\]\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">\\2</a>", $string);
			$string = preg_replace("#\[url\]www.(.*?)\[img\]www.(.*?)\[/img\]\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>", $string);    
			$string = preg_replace("#\[url=http://(.*?)\]\[img\]http://(.*?)\[/img\]\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">\\2</a>", $string);
			$string = preg_replace("#\[url=www.(.*?)\]\[img\]www.(.*?)\[/img\]\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>", $string);
		}
		
		$string = preg_replace("#\[url\]http://(.*?)\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">\\1</a>", $string);            // http://.......
		$string = preg_replace("#\[url\]www.(.*?)\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">http://www.\\1</a>", $string);    // www...........
		$string = preg_replace("#\[url=http://(.*?)\](.*?)\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">\\2</a>", $string);      // link
		$string = preg_replace("#\[url=www.(.*?)\](.*?)\[/url\]#si", "<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>", $string);     // link

		$string = preg_replace("#(^|[^\"=]{1})(http://|https://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm","\\1<a href=\"\\2\\3\" target=\"_blank\">\\3</a>\\4",$string);
		$string = preg_replace("#\[email\](.*?)\[/email\]#si", "<a href=\"mailto:\\1\">\\1</a>", $string);
		$string = preg_replace("#\[email=(.*?)\](.*?)\[/email\]#si", "<a href=\"mailto:\\1\">\\2</a>", $string);
		$string = preg_replace("#\[size=(.*?)\](.*?)\[/size\]#si", "<font size=\"\\1\">\\2</font>", $string);
		$string = preg_replace("#\[color=(.*?)\](.*?)\[/color\]#si", "<font color=\"\\1\">\\2</font>", $string);
		$string = preg_replace("#\[font=(.*?)\](.*?)\[/font\]#si", "<font face=\"\\1\">\\2</font>", $string);
		$string = preg_replace("#\[align=(.*?)\](.*?)\[/align\]#si", "<div align=\"\\1\">\\2</div>", $string);
		$string = preg_replace("#\[b\](.*?)\[/b\]#si", "<b>\\1</b>",$string); 
		$string = preg_replace("#\[i\](.*?)\[/i\]#si", "<i>\\1</i>",$string); 
		$string = preg_replace("#\[u\](.*?)\[/u\]#si", "<u>\\1</u>",$string); 
		$string = preg_replace("#\[s\](.*?)\[/s\]#si", "<s>\\1</s>",$string);

		return $string;
	}
I got started with something that I found Googleing, and then just duplicated the process for the rest of the my tags. I did this quite a long time ago, and it's probably not considered a good approach but, it does work.
__________________

Need website help? PM me!
CrazeD is offline  
Old 02-15-2009, 09:38 PM   #5 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: javascript regex generator

im putting this in php bbcode because im lazy

PHP Code:
function bbcode ($string$imgtag) {
        
$string preg_replace("#\[ quote=(.*?)\]#si""[ quote]<b>\\1 wrote:</b><br/>",$string); 
        
        
//prepare: how often start- and end-tag occurrs
        
$starttags substr_count($string'[ quote]');
        
$endtags substr_count($string'[ /quote]');
        
        
$overflow=abs($starttags-$endtags);
        
        for(
$i=0;$i<$overflow;$i++) {
            if(
$starttags>$endtags$string=$string.'[ /quote]';
            elseif(
$endtags>$starttags$string='[ quote]'.$string;
        }
            
        
$string preg_replace("#\[ quote\]#si"'<table width="95%" border="1" cellpadding="2" cellspacing="1" align="center"><tr><td class="gen">'$string10);
        
$string preg_replace("#\[ /quote\]#si"'</td></tr></table>',$string10); 
      
        
//remove overflowed quote-tags
      
        
$string=eregi_replace('\[ quote\]','',$string);
        
$string=eregi_replace('\[ /quote\]','',$string);

        if (
$imgtag == 1) {
            
$string preg_replace("#\[img align=(right)\]www.(.*?)\[/img\]#si""<img src=\"http://\\2\" border=\"0\" class=\"floatRight\" />"$string);
            
$string preg_replace("#\[img align=(right)\](.*?)\[/img\]#si""<img src=\"\\2\" border=\"0\" class=\"floatRight\" />"$string);
            
$string preg_replace("#\[img align=(left)\]www.(.*?)\[/img\]#si""<img src=\"http://\\2\" border=\"0\" class=\"floatLeft\" />"$string);
            
$string preg_replace("#\[img align=(left)\](.*?)\[/img\]#si""<img src=\"\\2\" border=\"0\" class=\"floatLeft\" />"$string);
            
$string preg_replace("#\[img\]www.(.*?)\[/img\]#si""<img src=\"http://\\1\" border=\"0\" />"$string);
            
$string preg_replace("#\[img\](.*?)\[/img\]#si""<img src=\"\\1\" border=\"0\" />"$string);
            
$string preg_replace("#\[img:(.*?)\](.*?)\[/img:(.*?)\]#si""<img src=\"\\2\" border=\"0\" />"$string);
            
$string preg_replace("#\[url\]http://(.*?)\[img\]http://(.*?)\[/img\]\[/url\]#si""<a href=\"http://\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>"$string);
            
$string preg_replace("#\[url\]www.(.*?)\[img\]www.(.*?)\[/img\]\[/url\]#si""<a href=\"http://www.\\1\" target=\"_blank\"><img src=\"http://www.\\2\" border=\"0\"></a>"$string);    
            
$string preg_replace("#\[url=http://(.*?)\]\[img\]http://(.*?)\[/img\]\[/url\]#si""<a href=\"http://\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>"$string);
            
$string preg_replace("#\[url=www.(.*?)\]\[img\]www.(.*?)\[/img\]\[/url\]#si""<a href=\"http://www.\\1\" target=\"_blank\"><img src=\"http://\\2\" border=\"0\"></a>"$string);        
        } else {
            
$string preg_replace("#\[img\](.*?)\[/img\]#si""\\1"$string);
            
$string preg_replace("#\[img:(.*?)\](.*?)\[/img:(.*?)\]#si""\\2"$string);
            
$string preg_replace("#\[url\]http://(.*?)\[img\]http://(.*?)\[/img\]\[/url\]#si""<a href=\"http://\\1\" target=\"_blank\">\\2</a>"$string);
            
$string preg_replace("#\[url\]www.(.*?)\[img\]www.(.*?)\[/img\]\[/url\]#si""<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>"$string);    
            
$string preg_replace("#\[url=http://(.*?)\]\[img\]http://(.*?)\[/img\]\[/url\]#si""<a href=\"http://\\1\" target=\"_blank\">\\2</a>"$string);
            
$string preg_replace("#\[url=www.(.*?)\]\[img\]www.(.*?)\[/img\]\[/url\]#si""<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>"$string);
        }
        
        
$string preg_replace("#\[url\]http://(.*?)\[/url\]#si""<a href=\"http://\\1\" target=\"_blank\">\\1</a>"$string);            // http://.......
        
$string preg_replace("#\[url\]www.(.*?)\[/url\]#si""<a href=\"http://\\1\" target=\"_blank\">http://www.\\1</a>"$string);    // www...........
        
$string preg_replace("#\[url=http://(.*?)\](.*?)\[/url\]#si""<a href=\"http://\\1\" target=\"_blank\">\\2</a>"$string);      // link
        
$string preg_replace("#\[url=www.(.*?)\](.*?)\[/url\]#si""<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>"$string);     // link

        
$string preg_replace("#(^|[^\"=]{1})(http://|https://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm","\\1<a href=\"\\2\\3\" target=\"_blank\">\\3</a>\\4",$string);
        
$string preg_replace("#\[email\](.*?)\[/email\]#si""<a href=\"mailto:\\1\">\\1</a>"$string);
        
$string preg_replace("#\[email=(.*?)\](.*?)\[/email\]#si""<a href=\"mailto:\\1\">\\2</a>"$string);
        
$string preg_replace("#\[size=(.*?)\](.*?)\[/size\]#si""<font size=\"\\1\">\\2</font>"$string);
        
$string preg_replace("#\[color=(.*?)\](.*?)\[/color\]#si""<font color=\"\\1\">\\2</font>"$string);
        
$string preg_replace("#\[font=(.*?)\](.*?)\[/font\]#si""<font face=\"\\1\">\\2</font>"$string);
        
$string preg_replace("#\[align=(.*?)\](.*?)\[/align\]#si""<div align=\"\\1\">\\2</div>"$string);
        
$string preg_replace("#\[b\](.*?)\[/b\]#si""<b>\\1</b>",$string); 
        
$string preg_replace("#\[i\](.*?)\[/i\]#si""<i>\\1</i>",$string); 
        
$string preg_replace("#\[u\](.*?)\[/u\]#si""<u>\\1</u>",$string); 
        
$string preg_replace("#\[s\](.*?)\[/s\]#si""<s>\\1</s>",$string);

        return 
$string;
    } 

office politics is offline  
Old 02-21-2009, 12:58 PM   #6 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: javascript regex generator

Learn Regular Expression (Regex) syntax with C# and .NET - Rad Software

good for understanding regex's
office politics is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript code & tutorials oldskool Programming Discussions 4 02-01-2009 05:20 PM
Using javascript to hide and unhide elements dynamically Osiris Programming Discussions 0 01-14-2009 08:56 PM
Firefox 3.1 beta arrives with JavaScript booster turned off Osiris Browser & General Internet Questions 3 10-15-2008 03:50 PM
The A-Z of Programming Languages: JavaScript Osiris Programming Discussions 0 08-04-2008 09:35 AM
PHP: How to modify a downloaded JavaScript code and partly change the code within it? LincolnX Programming Discussions 4 01-25-2008 04:25 PM