Use the replace () method in parsing Javascript

Source: Internet
Author: User

Do you know how to use the replace () method in Javascript? I 'd like to describe it here. I believe this article will surely help you gain some benefits.

Replace () method in Javascript

In javascript, the String function replace () is quite popular. Its flexible and powerful character replacement processing capability makes me want to introduce it to you.

Replace () is a simple replacement of characters. The sample code is as follows:

 
 
  1. <Scriptlanguagescriptlanguage = "javascript">
  2. VarstrM = "javascriptisagoodscriptlanguage ";
  3. // Here I want to replace letter a with Letter
  4. Alert (strM. replace ("a", ""));
  5. </Script>

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!

 
 
  1. <Scriptlanguagescriptlanguage = "javascript">
  2. VarstrM = "javascriptisagoodscriptlanguage ";
  3. // Here I want to replace letter a with Letter
  4. Alert (strM. replace (/a/, ""));
  5. </Script>

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.

 
 
  1. <Scriptlanguagescriptlanguage = "javascript">
  2. VarstrM = "javascriptisagoodscriptlanguage ";
  3. // Replace all letters a with letters
  4. Alert (strM. replace (/a/g, ""));
  5. </Script>

You can also look at the effect!

 
 
  1. <scriptlanguagescriptlanguage="javascript"> 
  2. varstrM="javascriptisagoodscriptlanguage";  
  3. alert(strM.replace(/(javascript)s*(is)/g,"$1$2fun.it$2"));  
  4. </script> 

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.

 
 
  1. <scriptlanguagescriptlanguage="javascript"> 
  2. varstrM="javascriptisagoodscriptlanguage";  
  3. functionchange(word)  
  4. {  
  5.  returnword.indexOf(0).toUpperCase()+word.substring(1);  
  6. }  
  7. alert(strM.replace(/w+/g,change));  
  8. </script> 

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.

 
 
  1. <scriptlanguagescriptlanguage="javascript"> 
  2. varstrM="javascriptisagoodscriptlanguage";  
  3. functionchange(word)  
  4. {  
  5.  varresult=word.match(/(w)/g);  
  6. if(result)  
  7. {  
  8.  varstr="";  
  9.  for(vari=result.length-1;i>=0;i--)  
  10.  {  
  11. str+=result;  
  12.  }  
  13.  returnstr;  
  14. }  
  15. else  
  16. {  
  17.  return"null";  
  18. }  
  19. }  
  20. alert(strM.replace(/(w)+/g,change));  
  21. </script> 

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.