One function commonly used for text input boxes is to replace specified characters. JavaScript has a very useful method replace(), which can be used to use the specified characters in an alternative character set. The
replace() method allows you to specify the character or set of characters you wish to replace by using a string or regular expression; this is the first argument to the method. The second argument is the character that Xie Ang is replaced with. The second argument is usually just a replacement string (replacement character set), but it can be a function that determines what the replacement string should be - if it is a function, the return value should be used as the replacement string. The syntax of hiding the replace() method can be any of the following:
string.replace(oldSubString,newSubString);
string.replace(regEx,newSubString);
string.replace(regEx,finction());
Simple example below Use the replace() method on a text area and look for the string "URL" in the text box. After finding the string "URL", the method replaces it with the string "ABC". Here is the example:
The replace() method Replacing character strings:
< form name="myForm">
Add a " " on both sides of the string "URL" to indicate word boundaries - indicating that you want to find the entire word - because the string "URL" will only be replaced if it is a stand-alone word (it cannot Only check whether there are spaces on both sides of the string "URL", because there may be punctuation around it);
oneclick="document.myForm.myTextArea.value=document.myForm.myTextArea.value.replace(/burlb/gi,'abc');"
The forward slashes around the string "URL" indicate that Correspondence is looking for a match of that string. The "g" after the second forward slash (called a flag) indicates that the document is looking for a global match in the entire text area (if there is no g flag, only the first match in the string is replaced), and the i flag indicates that it Should be a case-insensitive match (so the string "URL" will also be replaced, or indeed any mix of uppercase and lowercase of these characters will be replaced).
You can also use the "|" symbol to match multiple strings; the following example finds matches for link, url, or homepage:
/link| url| homepage/
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn