該方法找到一個正則表達式的字符串之間的匹配,并取代了匹配的子帶的新的子串。
替換字符串可以包含以下特殊替換模式:

語法
string.replace(regexp/substr, newSubStr/function[, flags]);
下面是參數的詳細信息:
返回值:
例子:
下面的示例演示了如何使用全球和忽略大小寫標志,允許替換,以使用字符串'oranges'取代'apples'
<html><head><title>JavaScript String replace() Method</title></head><body><script type="text/javascript">var re = /apples/gi;var str = "Apples are round, and apples are juicy.";var newstr = str.replace(re, "oranges");document.write(newstr ); </script></body></html>
 
例子:
下面的例子演示了如何在一個字符串轉換的詞:
<html><head><title>JavaScript String replace() Method</title></head><body><script type="text/javascript">var re = /(/w+)/s(/w+)/;var str = "zara ali";var newstr = str.replace(re, "$2, $1");document.write(newstr);</script></body></html>
新聞熱點
疑難解答