Regular expression syntax based on the replace function in javascript

Source: Internet
Author: User

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

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.