Today we talked about the character replacement function of JS replace the details, mainly about his replacement of simple characters and the use of regular expressions to deal with character replacement, there is a need for friends to see.
The simplest substitution function
<script language= "Web Effects" >
var strm = "JavaScript is a good script language";
Here I want to replace the letter A with the letter A
Alert (Strm.replace ("A", "a"));
</script>
The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.
Grammar
Stringobject.replace (regexp/substr,replacement) parameter description
Regexp/substr required. The RegExp object that prescribes the substring or pattern to be replaced.
Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object.
Replacement required. A string value. Provides a function that replaces text or generates alternate text.
<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
Alert (Strm.replace (/JavaScript) s* (IS)/g, "$ fun. It $"));
</script>
The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.
<script language= "JavaScript" >
var strm = "JavaScript is a www.111cn.net good script language";
function Change (Word)
{
Return Word.indexof (0). toUpperCase () +word.substring (1);
}
Alert (Strm.replace (/bw+b/g,change));
</script>