Regular expression syntax _ regular expressions in JavaScript based on the Replace function

Source: Internet
Author: User
The sample code is as follows:
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Here I want to replace the letter A with letter a alert (Strm.replace ("A", "a")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

It replaces only the first letter. But if you add regular expressions, the results are different! Replace () supports regular expressions, which match characters or strings according to the rules of regular expressions, and then give replacements!

Note: The replaced part does not need double quotes.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Here I want to replace the letter A with letter a alert (Strm.replace (/a/, "a")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

This only replaces the first letter A.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Replace the letter A with the letter a alert (Strm.replace (/a/g, "a")). </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

It is clear from the above that when a regular expression has a "G" flag, the representative will process the entire string.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Alert (Strm.replace (/JavaScript) \s* (IS)/g, "$ fun. It $")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Let's take a look at a simple example: capitalize all the first letters of the word.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; function Change (word) {return Word.indexof (0). toUpperCase () +word.substring (1); Alert (Strm.replace (/\b\w+\b/g,change)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

It is clear from the above that when a regular expression has a "G" flag, the representative will handle the entire string, that is, the transformation of the function change will be applied to all matching objects. The function has three or more parameters, depending on the regular expression.

With functions and regular expressions, the replace () processing string has an unprecedented power!

Finally, for example, it is so easy to reverse all the words in a string, using the replace () process.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; function Change (word) {var result = Word.match (/(\w)/g); if (result) {var str = ""; for (var i=result.length-1; i>=0; i--) {str = result; return str; else {return "null"; } alert (Strm.replace (/\b (\w) +\b/g,change)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Friends who are unfamiliar with regular expressions can refer to the following article
Regular expression 30-minute introductory tutorial
Regular expression base data
Related Article

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.