How to Use Replace () in Javascript

Source: Internet
Author: User

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:

<Script language = "JavaScript">

VaR STRM = "javascript is a good script language"; // here I want to replace letter A with the letter aalert (STRM. Replace ("A", ""));

</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!

<Script language = "JavaScript"> V

Ar STRM = "javascript is a good script language"; // here I want to replace letter A with the letter aalert (STRM. Replace (/A/, ""));

</SCRIPT>

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. <Script language = "JavaScript">

VaR STRM = "javascript is a good script language"; // replace all letters A with aalert (STRM. Replace (/A/G, "A") here "));

</SCRIPT>

You can also look at the effect!

<Script language = "JavaScript">

VaR STRM = "javascript is a good script language"; alert (STRM. replace (/(JavaScript)/S * (is)/g, "$1 $2 fun. it $2 "));

</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.

<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>

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.

<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>

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.