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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replace all instances and case insesitive from aray in a string

    I have an array...
    Code:
    var blockChars = new Array("!", "#", "*", "@", "th", "td");
    and a string which is user input...
    Code:
    var input = textbox.value;
    I want to be able to replace all instances and have it be case insensitive in the input string using the blockChars array. Replace with nothing ("")
    Example...
    User input is # is a th with td and a @ but even more TD minus one tH plus a #
    and the output would be is a with and a but even more minus one plus a

    How would I do that?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Code:
    var input=texbox.value.replace(/[!#*@]|(\bt[hd]\b)gi,'');
    Code:
    Assuming you want the th to remain in the word 'with', *****.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    @mrhoo, you’ve accidentally missed the closing delimiter
    Code:
    var input=texbox.value.replace(/[!#*@]|(\bt[hd]\b)/gi,'');
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by Dormilich View Post
    @mrhoo, you’ve accidentally missed the closing delimiter
    Code:
    var input=texbox.value.replace(/[!#*@]|(\bt[hd]\b)/gi,'');
    And mis-spelled textbox


    Code:
    <script type = "text/javascript">
    //var input = document.getElementById("textbox").value;
    var input =  "# is a th with td and a @ but even more TD minus one tH plus a #";  //test string
    input = input.replace(/[!#*@]/g,"");  // strip specified characters
    input = input.replace(/(\bt[hd]\b)/gi,"");  // strip td and/or th as whole words only
    input = input.replace(/\s{2,}/g," ");  // Replace multiple spaces with one space
    alert (input);
    </script>
    Last edited by Philip M; 01-05-2014 at 12:49 PM.

    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.


  •  

    Tags for this Thread

    Posting Permissions

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