<?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>omiga &#187; replace</title>
	<atom:link href="http://omiga.org/blog/archives/tag/replace/feed" rel="self" type="application/rss+xml" />
	<link>http://omiga.org/blog</link>
	<description>简单就好</description>
	<lastBuildDate>Thu, 26 Apr 2012 04:18:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>replace (searchValue, replaceValue)</title>
		<link>http://omiga.org/blog/archives/1316</link>
		<comments>http://omiga.org/blog/archives/1316#comments</comments>
		<pubDate>Tue, 02 Mar 2010 09:27:13 +0000</pubDate>
		<dc:creator>omiga</dc:creator>
				<category><![CDATA[前端开发]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://omiga.org/blog/?p=1316</guid>
		<description><![CDATA[字符串对象的replace方法作查找替换操作非常好用。 语　法 stringObject.replace(regexp,replacement) regexp &#8211; 必需。规定了要替换的模式的 RegExp 对象。请注意，如果该值是一个字符串，则将它作为要检索的直接量文本模式，而不是首先被转换为 RegExp 对象。 replacement &#8211; 必需。一个字符串值。规定了替换文本或生成替换文本的函数。 简单使用： var s = 'he is a god girl!'; var pattern = /he/g; alert(s.replace(pattern,"she")); 稍微复杂一点的使用：(回溯引用) var s = 'i like GFW'; var pattern = /(GFW)/g; alert(s.replace(pattern,"the great $1")); 当传入的替换文本参数为字符串时它还接受第三个参数 var str = "Apples are round, and apples are juicy."; alert(str.replace("apples", "oranges", "gi")); [...]]]></description>
			<content:encoded><![CDATA[<p>字符串对象的replace方法作查找替换操作非常好用。</p>
<blockquote><p><strong>语　法</strong><br />
stringObject.replace(regexp,replacement)</p>
<p>regexp &#8211; 必需。规定了要替换的模式的 RegExp  对象。请注意，如果该值是一个字符串，则将它作为要检索的直接量文本模式，而不是首先被转换为 RegExp 对象。</p>
<p>replacement &#8211; 必需。一个字符串值。规定了替换文本或生成替换文本的函数。</p></blockquote>
<p>简单使用：</p>
<pre>var s = 'he is a god girl!';
var pattern = /he/g;
alert(s.replace(pattern,"she"));</pre>
<p>稍微复杂一点的使用：(<a href="http://omiga.org/blog/archives/762" target="_blank">回溯引用</a>)</p>
<pre>var s = 'i like GFW';
var pattern = /(GFW)/g;
alert(s.replace(pattern,"the great $1"));</pre>
<p>当传入的<strong>替换文本参数为字符串时</strong>它还接受第三个参数</p>
<pre>var str = "Apples are round, and apples are juicy.";
alert(str.replace("apples", "oranges", "gi"));</pre>
<p>其实，这些都不是我想说的，我想说replacement为函数的情况。</p>
<blockquote><p>ECMAScript v3 规定，replace() 方法的参数 replacement  可以是函数而不是字符串。在这种情况下，每个匹配都调用该函数，它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数 是与模式中的子表达式匹配的字符串，可以有 0 个或多个这样的参数。接下来的参数是一个整数，声明了匹配在 stringObject  中出现的位置。最后一个参数是 stringObject 本身。</p></blockquote>
<p>根据以上说明，replace()可以通过为替换函数传入参数完成一些更为复杂的功能。</p>
<p>如将所有单词首字母转换成大写：</p>
<pre>var s = "the great gFW";
var pattern = /\b\w+\b/g;
alert(s.replace(pattern,function(w){return w.substring(0,1).toUpperCase()+w.substring(1)}))</pre>
<p>多传几个参数试试：</p>
<pre>var s = "AAAbbbZZZ AAAcccZZZ AAAdddZZZ AAAeeeZZZ hijklmnopqrstuvwxy";
var pattern = /\b(AAA)(\w+)(ZZZ)\b/g;
alert(s.replace(pattern,function(w,w1,w2,w3,i){return i+w1+w2+w3+i}));</pre>
<p>就这样了？可以继续传入参数吗？再试：</p>
<pre>var s = "AAAbbbZZZ AAAcccZZZ AAAdddZZZ AAAeeeZZZ hijklmnopqrstuvwxy";
var pattern = /\b(AAA)(\w+)(ZZZ)\b/g;
s.replace(pattern,function(w,w1,w2,w3,i,x){alert(x)});</pre>
<p>居然还有～···不过这个值从何而来我也迷惑了～···还可以传参数吗？再试！的确不可以了。</p>
<p>函数中第一个参数是匹配模式的字符串，接下来的参数是匹配模式中子表达式的字符串，数量与子表达式个数一致。当所有模式匹配参数都传递完后，后一个参数为匹配在字符串中出现的位置。根据这个特性可以发散出很多高级的应用。</p>
<hr />
<p><small>© omiga for <a href="http://omiga.org/blog">omiga</a>, 2010. |
<a href="http://omiga.org/blog/archives/1316">Permalink</a> |
<a href="http://omiga.org/blog/archives/1316#comments">3 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://omiga.org/blog/archives/1316&title=replace (searchValue, replaceValue)">del.icio.us</a>
<br/>
Post tags: <a href="http://omiga.org/blog/archives/tag/regexp" rel="tag">regexp</a>, <a href="http://omiga.org/blog/archives/tag/replace" rel="tag">replace</a>, <a href="http://omiga.org/blog/archives/tag/string" rel="tag">String</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://omiga.org/blog/archives/1316/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

