Replace () is a simple replacement of characters. Example Code As follows: <textarea id="runcode23652"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
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!
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
You must have discovered it. 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.
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
You can also look at the effect!
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
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.
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
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.
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]