The sample code is as follows:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
It only replaces the first letter. However, if you add a regular expression, the results will be different! Replace () supports regular expressions. It can match characters or strings according to the regular expression rules and then replace them!
Note: Do not add double quotation marks to the replaced part.
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
In this case, only the first letter a is replaced.
[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 the "g" flag, it means that the entire string will be processed.
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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]
If you are not familiar with regular expressions, refer to the following article.
Getting started with regular expressions in 30 minutes
Basic Regular Expressions