Do you know how to use the replace () method in Javascript? I 'd like to describe it here. I believe this article will surely help you gain some benefits.
Replace () method in Javascript
In javascript, the String function replace () is quite popular. Its flexible and powerful character replacement processing capability makes me want to introduce it to you.
Replace () is a simple replacement of characters. The sample code is as follows:
- <Scriptlanguagescriptlanguage = "javascript">
- VarstrM = "javascriptisagoodscriptlanguage ";
- // Here I want to replace letter a with Letter
- Alert (strM. replace ("a", ""));
- </Script>
I think you can see the result after running it. It only replaces the first letter. However, if you add a regular expression, the results will be different! Oh, that's right. Replace () supports regular expressions. It can match characters or strings according to the regular expression rules and then replace them!
- <Scriptlanguagescriptlanguage = "javascript">
- VarstrM = "javascriptisagoodscriptlanguage ";
- // Here I want to replace letter a with Letter
- Alert (strM. replace (/a/, ""));
- </Script>
In this case, only the first letter a is replaced. If you are familiar with regular expressions, it will be difficult for you. It will be OK after a slight modification.
- <Scriptlanguagescriptlanguage = "javascript">
- VarstrM = "javascriptisagoodscriptlanguage ";
- // Replace all letters a with letters
- Alert (strM. replace (/a/g, ""));
- </Script>
You can also look at the effect!
- <scriptlanguagescriptlanguage="javascript">
- varstrM="javascriptisagoodscriptlanguage";
- alert(strM.replace(/(javascript)s*(is)/g,"$1$2fun.it$2"));
- </script>
The examples here are all very simple applications. replace () is proportional to your ability to use regular expressions. The stronger your regular expression, the more crazy you will fall in love with it.
Of course, the reason why I recommend replace () is not because it can work with regular expressions, but because it can work with functions to provide powerful functions.
Let's take a look at a simple example: Replace the first letter of all words with uppercase letters.
- <scriptlanguagescriptlanguage="javascript">
- varstrM="javascriptisagoodscriptlanguage";
- functionchange(word)
- {
- returnword.indexOf(0).toUpperCase()+word.substring(1);
- }
- alert(strM.replace(/w+/g,change));
- </script>
As we can see from the above, when the regular expression has a "g" mark, it means that the entire string will be processed, that is, the transformation of the function change will be applied to all matching objects. This function has three or more parameters. The specific number depends on the regular expression.
With the combination of functions and regular expressions, replace () can process strings with unprecedented power!
In the end, it is easy to use replace () to process all words in the string in reverse order.
- <scriptlanguagescriptlanguage="javascript">
- varstrM="javascriptisagoodscriptlanguage";
- functionchange(word)
- {
- varresult=word.match(/(w)/g);
- if(result)
- {
- varstr="";
- for(vari=result.length-1;i>=0;i--)
- {
- str+=result;
- }
- returnstr;
- }
- else
- {
- return"null";
- }
- }
- alert(strM.replace(/(w)+/g,change));
- </script>