Use JS's replace () method to find descriptor code _javascript tips

Source: Internet
Author: User
A feature commonly used for text input boxes is to replace the specified characters. JavaScript has a very useful method of replace (), which can be used to specify characters by using alternative character set paths.

The Replace () method allows you to specify the character or character set that you want to replace, either by using a string or a regular expression, which is the first argument of the method. The second argument is the character that Cheans replaces. The second argument is usually just a replacement string (the replacement character set), but he can be a function to determine what the replacement string should be--if it is a function, the return value should be used as a Russian-Japanese replacement string. The syntax for hiding the replace () method can be any of the following:
Copy Code code 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 looks for a string "URL" in the text box. After the string "URL" is found, the method replaces it with the string "ABC". Here's the example:
Copy Code code as follows:

<body>
<p>replacing character strings:</p>
<form name= "MyForm" >
<textarea name= "Mytextarea" id= "Mytextarea" cols= "" rows= "a" >i am interested in Curl, and here's a URL for it.</t Extarea><br/><br/>
<input type= "button" value= "Replace characters URL" onclick= "Document.myForm.myTextArea.value = Document.myForm.myTextArea.value.replace (/\burl\b/gi, ' abc '); " >
</form>
</body>

Add a "\b" on both sides of the string "url" to indicate the bounds of the word--indicating that you want to find the whole word--because it is replaced only if the string "url" is a separate word (you cannot just check for spaces on either side of the string "url"). Because there may be punctuation marks around it);
Copy Code code as follows:

oneclick= "Document.myform.mytextarea.value=document.myform.mytextarea.value.replace (/\burl\b/gi, ' abc ');"

A forward slash around the string "URL" indicates that the correspondence is looking for a match for that string. The "G" (called a flag) behind the second forward slash indicates that the document is looking for a global match in the entire text area (if there are no G flags, replaces the first match in the string only), the I flag indicates that it should be a case-insensitive match (so the string "URL" will be replaced, too). Or, in fact, any mixed form of these characters in uppercase or lowercase is replaced.

You can also use the "|" The symbol matches multiple strings; The following example finds a match for link, url, or homepage:
Copy Code code 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.