Definitions and usage
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.
return value
A new string that is replaced with replacement for the first match of RegExp or after all matches have been made.
Example
The following example shows how the Replace method replaces the first occurrence of the word "the" with the use of the word "A".
The code is as follows |
Copy Code |
function Replacedemo () { var r, re; Declare a variable. var ss = "The man hit the ball with the BAT.N"; SS = "While the fielder caught the ball with the glove." re =/the/g; Creates a regular expression pattern. r = Ss.replace (Re, "A"); Replace "the" with "A". return (R); Returns the replaced string. } |
For more details please see: http://www.111cn.net/wy/99/js_replace_replaceall.htm
The above can only be replaced once, let's look at how to implement multiple substitutions
code is as follows |
copy code |
<script language= "JavaScript" $ (function () { var str = ' rtsp://112.5.172.36:10082/1d8a097f_51bf_4ef2_8380_ 71D63E9FB97A/C1V1.3GP '; var re =/://.*$/i; if (re.test (str)) { var newstr = str.substring (Str.indexof ("://") +3, Str.length ); var strarray = Newstr.split ("/"); alert (strarray[0].substring (0, Strarray[0].indexof (":")); alert (replacestr (strarray[1)); function replacestr (str) { var newstring = str; while (Newstring.indexof ("_") > 0) { newstring = Newstring.replace ("_", "-"); } return newstring; } }); </script> |