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/