Use the replace () method of js to find the character sample code _ javascript tips-js tutorial

Source: Internet
Author: User
JavaScript has a very useful method replace (), which can be used to take advantage of the character specified by the alternative character set tu huan. Below is a good example, you can try to replace the specified character in a text input box. JavaScript has a very useful method, replace (), which can be used to take advantage of the alternative character set.

The replace () method allows you to specify the character or character set to be replaced by a string or regular expression. This is the first real parameter of the method. The second real parameter is a character replaced by Xie Ang. The second real parameter is usually a replacement string (replacing the character set), but it can be used to determine what the replacement string should be. If it is a function, the return value should be replaced by a replacement string using the replacement string. The syntax for hiding the replace () method can be any of the following:

The Code is as follows:


String. replace (oldSubString, newSubString );
String. replace (regEx, newSubString );
String. replace (regEx, finction ());


The following simple example uses the replace () method for a text area and finds whether the text box contains a string "URL ". After finding the string "URL", this method replaces it with the string "ABC ". The following is an example:

The Code is as follows:



The replace () method

Replacing character strings:






Add a "on both sides of the string" URL "to indicate the limit of the word-indicating that the entire word is to be searched-because it is only used when the string" URL "is a separate word. replaced (you cannot only check whether there are spaces on both sides of the string "URL, because there may be punctuation marks around it );

The Code is as follows:


Oneclick = "document. myForm. myTextArea. value = document. myForm. myTextArea. value. replace (/\ burl \ B/gi, 'abc ');"


The forward slash around the string "URL" indicates that correspondence searches for matching of the string. The "g" (called a flag) after the second forward slash indicates that the document is looking for a global match in a real text area (if there is no g sign, only the first match in the string will be replaced ), the I flag indicates that it should be a case-insensitive match (so the string "URL" will be replaced, or in fact, any mix of uppercase and lowercase characters will be replaced ).

You can also use the '|' symbol to match multiple strings. The following example finds matching of link, url, or homepage:

The Code is as follows:


/Link | url | homepage/

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.