JavaScript 的 replace() 方法可以在字串中用一些字符替換另一些字符,或替換一個與正則運算式匹配的子串,
但是,只輸入字串的話,僅替換第一個字符,當然也可以用正則運算式來進行全域替換:
1 // 查找所有 word 替換成 words 2 string.replace(/word/g,"words");
那么,問題來了,如果我用的是變數呢?百度到可以這么來:
1 // 隨便來一條字串 2 let str = "How old are you? Yes, I'm very old!" 3 let search = "old"; 4 // 使用 new RegExp(pattern,modifiers) 創建正則運算式 5 let pattern = new RegExp(search, "g"); 6 let str = text.value.replace(pattern, "young"); 7 // 結果:How young are you? Yes, I'm very young!
但是,不用 new RegExp 自己寫一個函式,要怎么實作呢(我果然是太閑了)?
首先,摒棄掉 replace() 函式,自己來替換,
替換的話,不就是從前往后找想要替換的文并且一個個換掉嘛,
思路是這樣的,用 indexOf() 方法回傳指定的字串在字串中首次出現的位置,并用 slice(start, end) 方法提取找過但沒有匹配項的,匹配的文字和還沒找的三個部分,將第一部分和替換文字放入陣列中,還沒找的部分中再次使用這種辦法,直至字串末尾,將陣列連起來成為一條字串就是我想要的結果啦,
這是僅一次替換,咦,這不就是 replace() 嘛:
1 // 用于存放文字的陣列 2 let array = []; 3 let data; 4 // 查詢的文字第一次出現的位置 5 let start = oldText.indexOf(searchValue); 6 // 沒有找到匹配的字串則回傳 -1 7 if (start !== -1) { 8 // 查找的字串結束的位置 9 let end = start + searchValue.length; 10 // 添加沒有匹配項的字符,即從頭到查詢的文字第一次出現的位置 11 array.push(oldText.slice(0, start)); 12 // 添加替換的文字來代替查詢的文字 13 array.push(replaceValue); 14 // 剩下沒有查詢的文字 15 let remaining = oldText.slice(end, oldText.length); 16 // 這是結果 17 data = https://www.cnblogs.com/jmtm/p/array[0] + array[1] + remaining; 18 } else { 19 // 沒找到呀 20 data = "https://www.cnblogs.com/jmtm/p/No Found" + searchValue + "!"; 21 } 22 let textNode = document.createTextNode(data); 23 span.appendChild(textNode);
接下來進行全域替換,使用 while 回圈,判定條件就是 indexOf(searchValue) 是否能找到文字,回傳 -1 就停止回圈,
1 let array = []; 2 // 用于存放未查找的文字 3 let remaining = oldText; 4 let data; 5 let start = oldText.indexOf(searchValue); 6 while (start !== -1) { 7 let end = start + searchValue.length; 8 array.push(remaining.slice(0, start)); 9 array.push(replaceValue); 10 remaining = remaining.slice(end, remaining.length); 11 start = remaining.indexOf(searchValue); 12 } 13 14 // 這是結果 15 data = https://www.cnblogs.com/jmtm/p/array.join("") + remaining;
接著,再進一步,實作使用正則運算式來進行全域替換,大致思路是先找到正則匹配項,放入一個陣列,在回圈遍歷每一項,使用上面的方法進行全域替換,
要注意的是,替換的陣列不能有重復項,否則有可能出現問題,比如我想把 old 替換成 older,如果有兩個 old 在陣列中,最后的結果就會變成 olderer ,
1 /** 2 * 字串的全域替換 3 * @param oldText {string} 原始字串 4 * @param searchValue {string} 需要替換的字串 5 * @param replaceValue {string} 替換后的字串 6 * @returns {string} 回傳結果 7 */ 8 function replaceAll(oldText, searchValue, replaceValue) { 9 let result = oldText; 10 // 檢查是否是正則運算式 11 // 如果是正則運算式,則獲得匹配內容 12 let search; 13 if (searchValue) { 14 // 首先去掉空格 15 search = searchValue.match(/\S+/g)[0]; 16 // 匹配以 / 符號開頭 以 /img 形式結尾的內容 17 search = search.search(/^\/[\s\S]*?\/[img]$/g); 18 } else { 19 search = -1; 20 } 21 // 為了方便直接創建一個陣列用來存放需要替換的值 22 let searchArray = []; 23 if (search !== -1) { 24 let pattern = searchValue.slice(searchValue.indexOf("\/") + 1, searchValue.lastIndexOf("\/")); 25 let modifiers = searchValue.slice(searchValue.lastIndexOf("\/") + 1, searchValue.length); 26 // 防止正則寫的有問題,或者只是寫的像正則實際不是而導致的 nothing to repeat 報錯, 27 try { 28 search = oldText.match(new RegExp(pattern, modifiers)); 29 } catch (e) { 30 console.log(e); 31 // 報錯則默認為是需要替換的文本 32 search = null; 33 searchArray.push(searchValue); 34 } 35 if (search !== null) { 36 // 匹配成功后去掉重復項 37 search.forEach(function (item1) { 38 // if(searchArray.includes(item1)){} 39 // IE 不支持 array.includes() 所以自己寫一個回圈吧 40 // 陣列中有相同元素則為 true 41 let alreadyIn = false; 42 searchArray.forEach(function (item2) { 43 if (item1 === item2) { 44 alreadyIn = true; 45 } 46 }); 47 if (!alreadyIn) { 48 searchArray.push(item1); 49 } 50 }); 51 } else { 52 // 匹配失敗也默認為是需要替換的文本 53 searchArray.push(searchValue); 54 } 55 } else { 56 // 不是正則運算式也需要添加進陣列 57 searchArray.push(searchValue); 58 } 59 // 來回圈吧,把 search 里的每個元素換一遍,當然首先里面要有元素 60 if (searchValue) { 61 let remaining = result; 62 searchArray.forEach(function (item) { 63 // 將上一次替換結束的字串賦值給未掃描的字串變數 64 remaining = result; 65 let array = []; 66 let start = remaining.indexOf(item); 67 console.log(start); 68 // 沒有匹配項則回傳源字串 69 if (start === -1) { 70 result = remaining; 71 } 72 while (start !== -1) { 73 let end = start + item.length; 74 array.push(remaining.slice(0, start)); 75 array.push(replaceValue); 76 remaining = remaining.slice(end, remaining.length); 77 start = remaining.indexOf(item); 78 result = array.join("") + remaining; 79 } 80 }); 81 } 82 return result; 83 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/176002.html
標籤:JavaScript
上一篇:datatable的dom配置
下一篇:除錯webpack
