|
|
|
|
|
|
在JavaScript的字符替換方法中,大家常用的是replace()方法,而對于要求替換所有字符的方法,大家會常用replaceAll()來處理。在本文中,我將介紹3種方法,實現字符全替換。
1、replaceAll()方法
要實現字符全替換,當然replaceAll是首選的方法。
示例
var str = "apples are round, and apples are juicy";
str = str.replaceAll('apples','oranges');
console.log(str);
輸出
oranges are round, and oranges are juicy
replaceAll() 方法返回一個新字符串,新字符串所有滿足 pattern 的部分都已被replacement 替換。pattern可以是一個字符串或一個 RegExp, replacement可以是一個字符串或一個在每次匹配被調用的函數。
原始字符串保持不變。
示例
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replaceAll('dog', 'monkey'));
// 輸出:
// "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"
// 當用regex調用replaceAll,需要全局標志
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));
// 輸出:
//"The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
當使用一個 `regex`時,您必須設置全局(“ g”)標志,
否則,它將引發 TypeError:“必須使用全局 RegExp 調用 replaceAll”。
全部匹配由替代模式所取代的新的字符串。
此方法不會更改調用 String 對象。它只是返回一個新字符串。
在進行全局的搜索替換時,正則表達式需包含 g 標志。
你可以指定一個函數作為第二個參數。
在這種情況下,當匹配執行后,該函數就會執行。 函數的返回值作為替換字符串。另外要注意的是,如果第一個參數是正則表達式,并且其為全局匹配模式,那么這個方法將被多次調用,每次匹配都會被調用。
使用正則表達式搜索值時,它必須是全局的。這將行不通:
'aabbcc'.replaceAll(/b/, '.');
TypeError: replaceAll must be called with a global RegExp
這將可以正常運行:
'aabbcc'.replaceAll(/b/g, '.');
"aa..cc"
2、replace()方法
replace()方法也可以實現字符全替換,它的使用語法與replaceAll()相同。
一個部分或全部匹配由替代模式所取代的新的字符串。
但replace()僅替換第一個匹配項。
示例
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
// 輸出: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// 輸出: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
下面的例子中,正則表達式包含有全局替換(g)和忽略大小寫(i)的選項,這使得replace方法用'oranges'替換掉了所有出現的"apples"。
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
var newstr = str.replace(re, "oranges");
console.log(newstr);
// 輸出: oranges are round, and oranges are juicy.
3、使用 split() 和 join()
結合使用 split() 和 join() ,也能實現字符全替換。
示例
var str = "apples are round, and apples are juicy";
str = str.split('apples').join('oranges');
console.log(str);
輸出
oranges are round, and oranges are juicy
split() 方法用于把一個字符串分割成字符串數組。
split對特殊字符串的處理:
1、如果用“.”作為分隔的話,必須是如下寫法:String.split("\\."),這樣才能正確的分隔開,不能用String.split(".");
2、如果用“|”作為分隔的話,必須是如下寫法:String.split("\\|"),這樣才能正確的分隔開,不能用String.split("|");
3、如果用“\”作為分隔的話,必須是如下寫法:String.split(\\\),這樣才能正確的分隔開,不能用String.split("\");
“.”,“|”和“\”都是轉義字符,必須得加"\\";
如果想在串中使用"\"字符,則也需要轉義,例如首先要表達"aaaa\bbbb"這個串就應該用"aaaa\\bbbb",如果要分隔就應該這樣才能得到正確結果:
String[] aa = "aaa\\bbb\\bccc".split(\\\\);
join()方法用于把數組中的所有元素放入一個字符串,元素通過指定的分隔符進行分割。
語法
arrayObject.join(separator);
返回值
返回一個字符串,該字符串是通過把arrayObject的每個元素轉換為字符串,然后把這些字符串連接起來,在兩個元素之間插入separator字母串而生成的。
var arr = new Array(3);
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join()); //George,John,Thomas
document.write(arr.join(".")); //George.John.Thomas
本文通過3種方法的介紹,實現JS字符全替換。一般來說,使用replaceAll()方法最簡單直接,replace() 需用 /g 選項,而split()和Join()的配合,也同樣能實現相同的功能,在實際場景中可靈活使用。
相關文章
