Simple analysis _javascript techniques for JavaScript replace () method

Source: Internet
Author: User
Replace () The simplest is the ability to be a simple character replacement. 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 the letter A
Alert (Strm.replace ("A", "a"));
</script>
I think we can see the result when we run it, it only replaces the first letter. But if you add regular expressions, the results are different! Oh, yes. Replace () supports regular expressions, which match characters or strings according to the rules of regular expressions, and then give replacements!
<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
Here I want to replace the letter A with the letter A
Alert (Strm.replace (/a/, "a"));
</script>
Oh, you must have found out. This only replaces the first letter A. If you are familiar with the regular, then this will be difficult for you. Just a little change is OK.
<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
Replace the letter a all with the letter A
Alert (Strm.replace (/a/g, "a"));
</script>
You can also do this, see the effect!
<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
Alert (Strm.replace (/JavaScript) \s* (IS)/g, "$ fun. It $"));
</script>
The examples I have here are simple applications where replace () is proportional to your ability to use regular expressions at this point. Your regular expression is stronger, hehe, then you will be madly in love with it.
Of course, the reason I recommend replace () is not because it works with regular expressions, but because it can work with functions to play a powerful role.
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 goo D 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 selected note: If you need to introduce external JS need to refresh to perform]
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.