Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Philip M

Thread: Replacing all instances of string from a string in javascript

  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Replacing all instances of string from a string in javascript

    Hi All,

    I am getting a message text which will contain n number of occurance of below string

    PHP Code:
    /text/SOAP-ENV:Envelope/SOAP-ENV:Body/ServiceLevel/ServiceLevelRequest/Data/{}/ 
    I want to replace all the occurance of this string from my message text.

    Please let me know how I can achieve this. I tried many variations but no success.


    Thanks
    Rohit

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Try this (but I am not 100% sure what you are trying to achieve):-

    Code:
    <script type = "text/javascript">
    
    var text = "/text/SOAP-ENV:Envelope/SOAP-ENV:Body/ServiceLevel/ServiceLevelRequest/Data/{}/" ;
    text = text.replace(/\/text.+{}\//gi,"My message");
    alert (text);
    
    </script>
    This ought to replace all instances of the substring:-


    Code:
    <script type = "text/javascript">
    
    var text = "XYZ/text/SOAP-ENV:Envelope/SOAP-ENV:Body/ServiceLevel/ServiceLevelRequest/Data/{}/ABC/text/SOAP-ENV:Envelope/SOAP-ENV:Body/ServiceLevel/ServiceLevelRequest/Data/{}/JKL" ;
    text = text.replace(/}\//gi,"}/~");  // assumes the text contains no tildes
    var textsplit = text.split("~");
    var len = textsplit.length;
    for (var i = 0; i<len; i++) {
    textsplit[i] = textsplit[i].replace(/\/text.+{}\//gi," My message ");
    }
    newtext = textsplit.join("");
    alert (newtext);
    
    </script>
    Quizmaster: The Jungfrau is a well-known peak in which country?
    Contestant: China
    Last edited by Philip M; 04-02-2014 at 05:29 PM.
    Raj4Smile likes this.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Raj4Smile (04-04-2014)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    You could also do (more simply):-

    Code:
    <script type = "text/javascript">
    
    var text = "XYZ/text/SOAP-ENV:Envelope/SOAP-ENV:Body/ServiceLevel/ServiceLevelRequest/Data/{}/ABC/text/SOAP-ENV:Envelope/SOAP-ENV:Body/ServiceLevel/ServiceLevelRequest/Data/{}/JKL" ;
    
    text = text.replace(/\/text\/SOAP-ENV:Envelope\/SOAP-ENV:Body\/ServiceLevel\/ServiceLevelRequest\/Data\/{}\//gi," My Message ");
    
    alert (text);
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •