JavaScript replace () method to implement multiple substitutions

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.