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

    string replace problem?

    I am trying to replace the strings in a textarea, using:

    function replace(string,text,by)
    {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
    {
    newstr += replace(string.substring(i+txtLength,strLength),text,by,1);
    }
    return newstr;
    }

    The problem is when I have to test for the string to be deleted before calling the replace function.

    function makeText()
    {
    var str = "This is some text\n";
    document.all['textarea1'].value = str;
    }

    function delText()
    {
    var str = "some text\n";
    var newstr = document.all['textarea1'.value;

    if (newstr.match(str))
    {
    replace(newstr,str,'');
    document.all['textarea1'].value = newstr;
    }
    else
    alert("error");
    }

    If str = "some text\n", the match is not found.
    If str = "some text", the newline is left in the textarea.

    I am using

    newstr = replace(replace(newstr,result,'\n'),'\n\n','');

    in another part of the program successfully.

    Please help.

    wolf

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i don't know if you have some reason to build your own function for this, but there's a method of strings for replacing text.

    string.replace(original, replacement);

    the only thing that i noticed in your functions was that in the following line, you have an opening bracket, but no closing one:
    var newstr = document.all['textarea1'.value;

    i suspect that this probably has something to do with the match() method. i'm in the middle of packing, so i can't check now, but tomorrow, i'll dig out my copy of the Guide ( see my signature ), and see if there's maybe something there that explains why you're having trouble.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    any help?

    ?

  • #4
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    well, as best i can make out, your problem does in fact come from using match. match is meant to be used with regular expressions, and you're using a string, which shouldn't mean anything, but apparently it does. try just checking for the existence of the string using indexOf. or, like i suggested before, just use the built in method.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Been fiddling with this String method for a bit; you're welcome to it if it serves your needs. Can't quite figure out the above.


    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="JavaScript">

    String.prototype.replaceChars = function(old_str, new_str) {
    var badChars = [/\//g , /\\/g , /\|/g , /\./g , /\(/g , /\)/g , /\$/g , /\[/g , /\^/g , /\*/g , /\\'/g , /\"/g , /\+/g , /\?/g];
    var goodChars = ['\/' , '\\\\\' , '\\|' , '\\.' , '\\(' , '\\)' , '\\$' , '\\[' , '\\^' , '\\*' , '\\\\'' , '\\"' , '\\+' , '\\?'];
    for (var i=0; i<badChars.length; ++i) old_str = old_str.replace(badChars[i], goodChars[i]);
    var RE = new RegExp(old_str, 'g');
    return this.replace(RE, new_str);
    }

    </script>
    </head>
    <body>
    <form>
    <textarea name="t1" rows="20" cols="80" wrap="virtual">
    The whales had to be pushed off in bunches, he said. Individuals pushed off are likely to try to rejoin the group on the beach. The carcasses of the nine dead whales were being removed from the beach one by one. Technically pilot whales are big dolphins. They range from 12 feet to 25 feet long and from 1.5 tons to 3 tons or more for adults. They are also known as blackfish. Strandings of pilot whales are not unusual on Cape Cod and at a few other locations around the globe. Scientists are still speculating on the reasons for such strandings. While there is no firm conclusion, it is generally believed the whales play follow the leader when the lead whale in a pod becomes disoriented due to illness or gets lost in the shallow water.
    </textarea>
    <input type="button" value="Replace 'whales' with 'idiots'"
    onclick="t1.value=t1.value.replaceChars('whales', 'idiots')"><br><br>
    <input type="button" value="Replace 'idiots' with 'sex-crazed penguins'"
    onclick="t1.value=t1.value.replaceChars('idiots', 'sex-crazed penguins')"><br><br>
    <input type="button" value="Replace '. ' with '\n'"
    onclick="t1.value=t1.value.replaceChars('. ', '.\n')">
    </form>
    </body>
    </html>
    Last edited by adios; 07-30-2002 at 04:27 AM.

  • #6
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks

    thanks for the help joh6nn and adios.


  •  

    Posting Permissions

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