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 use the characters specified by the alternate character set path.
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: