Detailed description of JavaScript Replace () usage with instance code

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.