<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Snippets &#187; regex</title>
	<atom:link href="http://www.randomsnippets.com/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to validate email format via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 05:30:08 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[checkEmail]]></category>
		<category><![CDATA[emailRegEx]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[javascript demo]]></category>
		<category><![CDATA[javascript email]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[valid email address]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[verifyEmail]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=13</guid>
		<description><![CDATA[
This JavaScript demo verifies that an email address is in the correct format and that the user has typed in the same address in both fields to prevent mistyping of the address.  I have borrowed the regex for verifying the correct email format from a different site. 

Demo

Email:



Please type in your email again:






Here is [...]]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/verifyEmail.js" language="Javascript" type="text/javascript"></script><br />
This JavaScript demo verifies that an email address is in the correct format and that the user has typed in the same address in both fields to prevent mistyping of the address.  I have <a href="http://www.regular-expressions.info/email.html" target="_blank">borrowed the regex for verifying the correct email format</a> from a different site. </p>
<fieldset>
<legend>Demo</legend>
<form name="myform">
Email:<br />
<input type="text" name="email1">
<p>
<p>
Please type in your email again:<br />
<input type="text" name="email2">
<p>
<input type="button" onclick="verifyEmail();" value="Check Email Address">
</form>
</fieldset>
<p><span id="more-13"></span><br />
Here is the plain HTML of the form.  Please note that I have removed the action attribute because this is only a demo and we are only concerned with the client-side verification of the JavaScript function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myform&quot;&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email1&quot;&gt;
&lt;p&gt;&lt;p&gt;
Please type in your email again: &lt;input type=&quot;text&quot; name=&quot;email2&quot;&gt;
&lt;p&gt;&lt;p&gt;
&lt;input type=&quot;button&quot; onClick=&quot;verifyEmail();&quot; value=&quot;Check Email Address&quot;&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Nothing exciting here except in line 6 where the <b>onClick</b> event is triggered when the user clicks on the button.  This event will launch the <b>verifyEmail()</b> JavaScript function which will check the user input of the email address.</p>
<p>Here is the code for the <b>verifyEmail()</b> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> verifyEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span>;     
<span style="color: #003366; font-weight: bold;">var</span> emailRegEx <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i</span>;
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span>.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span>emailRegEx<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter a valid email address.&quot;</span><span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">!=</span> document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email2</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Email addresses do not match.  Please retype them to make sure they are the same.&quot;</span><span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Woohoo!  The email address is in the correct format and they are the same.&quot;</span><span style="color: #009900;">&#41;</span>;
          <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">status</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function will return a <b>false</b> value if the user does not type in an email address in the correct format or if the user fails to type in the same email address in both fields.  If the email addresses passes both these tests, then the function will return a <b>true</b> value.</p>
<p>The <a href="http://www.regular-expressions.info/email.html" target="_blank">regex for validating the correct email format in line 2 has been borrowed</a> and edited slightly by adding the <b>i</b> flag so that the regex is case-insensitive.</p>
<p>The <b>if</b> block in lines 3-6 checks to see if the email address in the first field passes the regex check by invoking the <b>search()</b> method on our email string value.  This method accepts regex expressions as arguments to check the string and returns the position of the specified value in the string or a <b>-1</b> if no match was found.  If the <b>search()</b> method returns a <b>-1</b>, then an alert would tell the user that an invalid email address has been entered and returns a <b>false</b> value.</p>
<p>Lines 7-10 checks to see if the user has entered the same string in both email fields.  This is done by a simple equality check on the string values.  If the strings are different, an alert warns the user that the email addresses are not the same and returns a <b>false</b> value.</p>
<p>If we have passed the two tests, then <b>else</b> block in lines 11-14 gets executed.  An alert tells the user that he or she has done a good job and the function returns a <b>true</b> value so that you  can continue with the form submission.</p>
<p>Ideally, you would have this function run during the <b>onSubmit</b> attribute for the <b>form</b> tag.  This way, the form will get submitted when the <b>checkEmail()</b> function returns a <b>true</b> value.  Here is an example of how your form would look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myForm&quot; method=&quot;POST&quot; action=&quot;myTargetFile&quot; onSubmit=&quot;return checkEmail()&quot;&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email1&quot;&gt;
&lt;p&gt;&lt;p&gt;
Please type in your email again: &lt;input type=&quot;text&quot; name=&quot;email2&quot;&gt;
&lt;p&gt;&lt;p&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to find and replace text dynamically via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 07:18:20 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[find and replace]]></category>
		<category><![CDATA[findMyText]]></category>
		<category><![CDATA[haystack]]></category>
		<category><![CDATA[haystackText]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[Replacement]]></category>
		<category><![CDATA[replacement text]]></category>
		<category><![CDATA[text replacement]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/</guid>
		<description><![CDATA[
This is a neat little script that demonstrates two things:
1) Find &#8211; If only the Find box is filled in, then the findMyText() JavaScript function will just perform a find for the div that is given.  If there is a match, the text will be highlighted and marked in bold.
2) Find and Replace &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/findAndReplace.js" language="Javascript" type="text/javascript"></script><br />
This is a neat little script that demonstrates two things:</p>
<p>1) <b>Find</b> &#8211; If only the <b>Find</b> box is filled in, then the <b>findMyText()</b> JavaScript function will just perform a find for the div that is given.  If there is a match, the text will be highlighted and marked in bold.</p>
<p>2) <b>Find and Replace</b> &#8211; If both the <b>Find</b> and <b>Replace</b> boxes are filled in, then the <b>findMyText()</b> JavaScript function will do just that. </p>
<fieldset>
<legend>Demo</legend>
<div id="haystack">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<p></p>
<table>
<tr>
<td>Find</td>
<td>
<input id="needle" name="needle" type="text"></td>
</tr>
<tr>
<td>Replacment</td>
<td>
<input id="replacement" name="replacement" type="text"></td>
</tr>
</table>
<input type="button" value="Find" onClick="findMyText(document.getElementById('needle').value, document.getElementById('replacement').value);">
</fieldset>
<p><span id="more-11"></span><br />
Here is the plain HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;haystack&quot;&gt;
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
&lt;/div&gt;
&lt;br&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Find&lt;/td&gt;&lt;td&gt;&lt;input id=&quot;needle&quot; name=&quot;needle&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Replacment&lt;/td&gt;&lt;td&gt;&lt;input id=&quot;replacement&quot; name=&quot;replacement&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;input type=&quot;button&quot; value=&quot;Find&quot; onClick=&quot;findMyText(document.getElementById('needle').value, document.getElementById('replacement').value);&quot;&gt;</pre></td></tr></table></div>

<p>
Please excuse the use of the table for the text inputs and labels.  WordPress does some automatic formatting which is annoying sometimes but it is still a great application =)  The only thing of importance here is on line 9 where the <b>onClick</b> event executes the <b>findMyText()</b> function and passes 2 arguments:</p>
<ul>
<li><b>Needle</b> &#8211; This is the text that we are trying to find in the haystack of text.</li>
<li><b>Replacement</b> &#8211; This is our replacement text for any needle that we find.</li>
</ul>
<p>Here is our JavaScript function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> haystackText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
<span style="color: #003366; font-weight: bold;">function</span> findMyText<span style="color: #009900;">&#40;</span>needle<span style="color: #339933;">,</span> replacement<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>haystackText.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          haystackText <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;haystack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #003366; font-weight: bold;">var</span> match <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span>needle<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;ig&quot;</span><span style="color: #009900;">&#41;</span>;     
     <span style="color: #003366; font-weight: bold;">var</span> replaced <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>replacement.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          replaced <span style="color: #339933;">=</span> haystackText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> replacement<span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> boldText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;div style=<span style="color: #000099; font-weight: bold;">\&quot;</span>background-color: yellow; display: inline; font-weight: bold;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #339933;">+</span> needle <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/div&gt;&quot;</span>;
          replaced <span style="color: #339933;">=</span> haystackText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> boldText<span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;haystack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> replaced;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Like many of the scripts that you see here, using the JavaScript DOM objects is the key to doing the find and replace dynamically for our website.  </p>
<p>The script asks for two parameters: the needle which we are looking for in our text and the text we want to replace it with.  Please note that the replacement text is totally <i>optional</i>.  If no replacement text is given, then the behavior of the function changes a bit.</p>
<p>The <b>haystackText</b> variable is outside of the function in line 1 because we want to store the original text and always base our searches on the original text.  In lines 3-5 is where we store the original text in our <b>haystack</b> div if it is not already set.</p>
<p>Line 6 creates a regular expression object where we place our <b>needle</b> variable along with two flags:</p>
<ul>
<li><b>i</b> &#8211; This flag causes the regular expression match to be case-insensitive.</li>
<li><b>g</b> &#8211; This flag causes the regular expression match to be global so it matches every occurrence instead of just the first one.</li>
</ul>
<p>Lines 8-10 checks to see if the <b>replacement</b> text was set.  If so, we will use the <b>replace</b> method on our <b>haystackText</b> string object and feed it our <b>match</b> and <b>replacement</b> texts.  The resulting string will be saved in the <b>replaced</b> variable.</p>
<p>If the <b>replacement</b> text was not set, the <b>else</b> block in lines 11-14 will be executed.  We first prepare a variable of our <b>needle</b> that has a bit of extra styling such as a yellow background and bold text so it stands out better.  Next, we just use the <b>replace</b> method on our <b>haystackText</b> string object and pass the <b>match</b> and <b>replacement</b> texts.  The resulting string will be saved in the <b>replaced</b> variable.  </p>
<p>Lastly, the <b>replaced</b> variable which contains our results is used to substitute what was originally in our <b>haystack</b> div.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
