文章目錄
javascript中replace()用法詳解
在javascript中,String的函數replace()簡直太讓人喜愛了。它靈活而強大的字元替換處理能力,讓我不禁想向大家介紹它。
replace()最簡單的算是能力就是簡單的字元替換。
範例程式碼如下:
<script language="javascript">
var strM = "javascript is a good script language";//在此我想將字母a替換成字母Aalert(strM.replace("a","A"));
</script>
我想大家運行後可以看到結果,它只替換了首字母。但如果加上Regex結果就不一樣了!呵呵,沒錯。replace()支援Regex,它可以按照Regex的規則匹配字元或字串,然後給予替換!
<script language="javascript">v
ar strM = "javascript is a good script language";//在此我想將字母a替換成字母Aalert(strM.replace(/a/,"A"));
</script>
呵呵,您一定發現了。這樣還是只替換了第一個字母a。如果您熟悉正則,那這就難不住您。稍加修改就OK了。<script language="javascript">
var strM = "javascript is a good script language";//在此將字母a全部替換成字母Aalert(strM.replace(/a/g,"A"));
</script>
還可以這樣,看看效果!
<script language="javascript">
var strM = "javascript is a good script language";alert(strM.replace(/(javascript)/s*(is)/g,"$1 $2 fun. it $2"));
</script>
我這兒舉的例子都是很簡單的應用,replace()在此點上與您使用Regex的能力成正比。您Regex越強,呵呵,那您就會越瘋狂的愛上它。 當然,我這兒推薦replace()的原因並不因為它能與Regex合作,而在於它還能與函數進行合作,發揮出強大的功能。
先看看簡單例子:將所有單詞首字母換成大寫。
<script language="javascript">
var strM = "javascript is a good script language";function change(word){ return word.indexOf(0).toUpperCase()+word.substring(1);}alert(strM.replace(//b/w+/b/g,change));
</script>
由上可知,當Regex有"g"標誌時,代表將處理整個字串,即函數change的變換將應用於所有匹配的對象。而該函數有三個或更多參數,具體個數視Regex而定。 有了函數與Regex的配合,replace()處理字串的功能空前強大起來了! 最後還舉個例子,將字串所有單詞倒序,用replace()處理是如此簡單。
<script language="javascript">
var strM = "javascript is a good script language";
function change(word)
{ var result = word.match(/(/w)/g);if ( result ){ var str = "";
for ( var i=result.length-1; i>=0; i-- )
{ str += result; }
return str;
}
else{ return "null";}}
alert(strM.replace(//b(/w)+/b/g,change));
</script>
========================================================
分割線
========================================================
match() 定義和用法
match() 方法可在字串內檢索指定的值,或找到一個或多個Regex的匹配。
該方法類似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字串的位置。
文法
stringObject.match(searchvalue)stringObject.match(regexp)
參數 |
描述 |
searchvalue |
必需。規定要檢索的字串值。 |
regexp |
必需。規定要匹配的模式的 RegExp 對象。如果該參數不是 RegExp 對象,則需要首先把它傳遞給 RegExp 建構函式,將其轉換為 RegExp 對象。 |
傳回值
存放匹配結果的數組。該數組的內容依賴於 regexp 是否具有全域標誌 g。
說明
match() 方法將檢索字串 stringObject,以找到一個或多個與 regexp 匹配的文本。這個方法的行為在很大程度上有賴於 regexp 是否具有標誌 g。
如果 regexp 沒有標誌 g,那麼 match() 方法就只能在 stringObject 中執行一次匹配。如果沒有找到任何匹配的文本, match() 將返回 null。否則,它將返回一個數組,其中存放了與它找到的匹配文本有關的資訊。該數組的第 0 個元素存放的是匹配文本,而其餘的元素存放的是與Regex的子運算式匹配的文本。除了這些常規的數組元素之外,返回的數組還含有兩個對象屬性。index 屬性聲明的是匹配文本的起始字元在 stringObject 中的位置,input 屬性聲明的是對 stringObject 的引用。
如果 regexp 具有標誌 g,則 match() 方法將執行全域檢索,找到 stringObject 中的所有匹配子字串。若沒有找到任何匹配的子串,則返回 null。如果找到了一個或多個匹配子串,則返回一個數組。不過全域匹配返回的數組的內容與前者大不相同,它的數組元素中存放的是 stringObject 中所有的匹配子串,而且也沒有 index 屬性或 input 屬性。
注意:在全域檢索模式下,match() 即不提供與子運算式匹配的文本的資訊,也不聲明每個匹配子串的位置。如果您需要這些全域檢索的資訊,可以使用 RegExp.exec()。
執行個體例子 1
在本例中,我們將在 "Hello world!" 中進行不同的檢索:
<script type="text/javascript">var str="Hello world!"document.write(str.match("world") + "<br />")document.write(str.match("World") + "<br />")document.write(str.match("worlld") + "<br />")document.write(str.match("world!"))</script>
輸出:
worldnullnullworld!
例子 2
在本例中,我們將使用全域匹配的Regex來檢索字串中的所有數字:
<script type="text/javascript">var str="1 plus 2 equal 3"document.write(str.match(/\d+/g)
)</script>
輸出:
1,2,3
=====================================================
分割線
=====================================================
javascript中使用replaceAll()的方法(轉)
第一次發現JavaScript中replace() 方法如果直接用str.replace("-","!") 只會替換第一個匹配的字元.
而str.replace(/\-/g,"!")則可以全部替換掉匹配的字元(g為全域標誌)。
replace()
The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,
var s = "Hello. Regexps are fun."
;s = s.replace(/\./, "!" ); // replace first period with an exclamation pointalert(s);
produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,
var s = "Hello. Regexps are fun."
;s = s.replace(/\./g, "!" ); // replace all periods with exclamation pointsalert(s);
yields this result: “Hello! Regexps are fun!”
所以可以用以下幾種方式.:
string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);
string:字串運算式包含要替代的子字串。
reallyDo:被搜尋的子字串。
replaceWith:用於替換的子字串。
Js代碼 view source
print?
01 |
<script type= "text/javascript" > |
03 |
String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) { |
05 |
if (!RegExp.prototype.isPrototypeOf(reallyDo)) { |
07 |
return this .replace( new RegExp(reallyDo, (ignoreCase ? "gi" : "g" )), replaceWith); |
11 |
return this .replace(reallyDo, replaceWith); |