<?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>Web Cash &#187; string</title>
	<atom:link href="http://www.earn-web-cash.com/tag/string/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Javascript Strings: How to Do String Replacement in JS</title>
		<link>http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/</link>
		<comments>http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 20:55:31 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/</guid>
		<description><![CDATA[This morning I set about making a simple script to clean extra newline characters out of text. I could easily have processed the form contents in PHP, but I wanted to use Javascript to avoid reloading the page. The concept seemed simple enough, but I ran into a problem &#8211; I didn&#8217;t know how to [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I set about making a simple <a href="http://www.earn-web-cash.com/online-tools-design/text-cleaner-form/">script to clean extra newline characters</a> out of text.  I could easily have processed the form contents in PHP, but I wanted to use Javascript to avoid reloading the page.</p>
<p>The concept seemed simple enough, but I ran into a problem &#8211; I didn&#8217;t know how to do string replacement in Javascript.  The first thing I thought was, &#8220;What is the Javascript equivalent to str_replace?&#8221;<br />
<span id="more-152"></span></p>
<h5>The JS str_replace &#8211; String.replace()</h5>
<p>As it turns out, the closest thing in Javascript to str_replace is the replace method of the String class.  It&#8217;s actually more akin to preg_replace &#8211; since it uses regular expressions &#8211; but it&#8217;s the closest thing I could find for a built-in string replacement function.</p>
<p>It&#8217;s implementation is pretty simple.  Create a new string object.  Then call the replace method with two parameters &#8211; a regular expression to match and a string to replace it with.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> hello <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> goodbye <span style="color: #339933;">=</span> hello.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/hello/i</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Goodbye'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example, we do a simple search for the word &#8216;hello&#8217; (the &#8216;i&#8217; signifies that the search is case insensitive).  When we find &#8216;hello,&#8217; we replace it with goodbye.</p>
<h5>Using Variable Values Inside Regex</h5>
<p>That&#8217;s simple enough.  However, what if you don&#8217;t want to hard-code the regular expression?</p>
<p>I was left wondering how I could use a variables value within the regular expression for the search.  If I placed the variable inside the / / boundaries of the regex, it would simply search for the name of the variable.</p>
<p>I eventually stumbled across the RegExp class &#8211; which incidentally makes it easier to build a regex expression.  You create an instance of the RegExp class by passing it two parameters &#8211; the string to match and any modifiers (&#8216;i&#8217; or &#8216;g&#8217;) that should be used for the search.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> hello <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> matchWord <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>matchWord<span style="color: #339933;">,</span> <span style="color: #3366CC;">'i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> goodbye <span style="color: #339933;">=</span> hello.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> <span style="color: #3366CC;">'Goodbye'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This has the same effect as the last example &#8211; except now the string to match comes from the variable matchWord.  With this, you could use some type of user input to determine what to match and replace.</p>
<h5>Recap &#8211; Replacing Strings in Javascript</h5>
<p>To recap&#8230;</p>
<ul>
<li>There is no direct equivalent to str_replace in Javascript.</li>
<li>Use String.replace &#8211; which is very similar to preg_replace.</li>
<li>Two parameters &#8211; a regex to match and a string to replace it with.</li>
<li>Build an instance of the RegExp class to use variable values in your regex.</li>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

