JavaScript writes a replaceAll() function by itself

Keywords: Javascript

JavaScript's replace() method can replace some characters in a string or a substring that matches a regular expression.

However, if only a string is input, only the first character can be replaced. Of course, regular expressions can also be used for global replacement:

1 // Find all word replace with words
2 string.replace(/word/g,"words");

So, the question is, what if I'm using variables? Baidu can come here:

1 // Any string
2 let str = "How old are you? Yes, I'm very old!"
3 let search = "old";
4 // Use new RegExp(pattern,modifiers) Create regular expression
5 let pattern = new RegExp(search, "g");
6 let str = text.value.replace(pattern, "young");
7 // Result: How young are you? Yes, I'm very young!

But how do I write a function without new RegExp?

First, discard the replace() function and replace it yourself.

If you want to replace it, you should look for the text you want to replace and replace it one by one.

The idea is to use the indexOf() method to return the position of the first occurrence of the specified string in the string, and use the slice(start, end) method to extract the matched text and the three parts that have been found but have not been found, put the first part and the replacement text into the array, and use this method again in the parts that have not been found, until the end of the string, and array Connecting to a string is what I want.

This is only one replacement. Eh, this is replacement():

 1 // Array for storing text
 2 let array = [];
 3 let data;
 4 // Where the text of the query first appears
 5 let start = oldText.indexOf(searchValue);
 6 // If no matching string is found, return -1 
 7 if (start !== -1) {
 8     // Where to end the searched string
 9     let end = start + searchValue.length;
10     // Add characters that do not have a match, that is, where the first occurrence of text from the beginning to the query occurs
11     array.push(oldText.slice(0, start));
12     // Add alternate text to replace query text
13     array.push(replaceValue);
14     // No query text left
15     let remaining = oldText.slice(end, oldText.length);
16     // This is the result.
17     data = array[0] + array[1] + remaining;
18 } else {
19     // I didn't find it.
20     data = "No Found" + searchValue + "!";
21 }
22 let textNode = document.createTextNode(data);
23 span.appendChild(textNode);

Next, the global replacement is performed. Use the while loop to determine whether the indexOf(searchValue) can find the text. Return - 1 to stop the loop.

 1 let array;
 2 // Used to store unseen text
 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 if (array){
14     // This is the result.
15     data = array.join("") + remaining;
16 } else {
17     // I didn't find it.
18     data = "No Found " + searchValue + "!";
19 }

Posted by drdokter on Wed, 06 Nov 2019 06:24:20 -0800