www.webdeveloper.com
Results 1 to 6 of 6

Thread: Issue in searching for word in sentence and replacing it

  1. #1
    Join Date
    Nov 2011
    Location
    Corvallis Oregon
    Posts
    12

    Angry Issue in searching for word in sentence and replacing it

    Ok here is the situation. Using a series of sentences stored in a string, in this case called "str", I have to write some code to look through each sentence, and if the sentence does NOT contain the word "arr" then, with a 50/50 chance add "Arr" to the beginning of the sentence... With the code I have below, I believe I am very close.

    when the program is run, it displays the following;

    ,72,94 The rain in Spain stays mainly in the plain. I like nachos. I am batman. Well hello there arr. 0 The rain in Spain stays mainly in the plain. Arr I like nachos. I am batman


    this is just outputting some of the data so I can see what is going on.

    the first part, that reads ",72,94" is the string that contains the index values locating all of the periods in the sentence. However it only displays the last 2 values. The next part that reads "The rain in Spain stays mainly in the plain. I like nachos. I am batman. Well hello there arr." is the original sentence stored in the "str". I display this to make sure that the origonal data is not molested as the loop runs. so this checks out. The number 0 is the random number value, and each time the page is refreshed it will display a number between 0 and ten.

    This is all displaying the data and results I am looking for...

    the problem is with the last data set that diaplays "The rain in Spain stays mainly in the plain. Arr I like nachos. I am batman "

    as you can see the replacement is working so far... the second sentence now starts with "Arr" and if you refresh the beginning of the second and third sentences have a 50/50 chance of having this "Arr" Yet the last sentence is dropped... I suspect it is because of the way I got the index values. I am not sure, but I believe that as each pass goes through, and each time the sting containing the comma separated index values (periodIndex) has the first value removed, it hits a problem when only one variable is available.. So when it tries to create the "second" variable based on data from the index2 and index3 (which are the index locations of the commas on each side of the number in the string "periodIndex") variables, it stops when there is no index 3 (because eventually there is only a single variable left; 94, and so the variable index3 returns null)


    I need this to be able to apply this "arr" filter on any sentence or series of sentences that are fetched from the "str" string. Any help understanding why it is dropping the last sentence would be greatly appreciated


    HTML Code:
    <html>
    <body>
    
    <script type="text/javascript">
    var str="The rain in Spain stays mainly in the plain. I like nachos. I am batman. Well hello there arr.";
    
    var patt1=/\./g; //used to search for periods
    var periodIndex = ""; //assigns blank variable for later use
    
    
    while (patt1.test(str)==true) //loop creates list in periodIndex that includes the index locations of each period, seperated by commas
      {
    	periodIndex +=  "," + patt1.lastIndex;
      };
    // below this line is the code to check each sentence
    var str2 = str.substring(0,str.indexOf("."));  //creates temporary string that contains first sentence, as it does not have a period in front of it
    for (var j = 0; j < periodIndex.length; j=j+5) { 
    var randomNumber=Math.floor(Math.random()*11) //creates a random number between 0 and ten
    var index1 = periodIndex.indexOf(",");  //creates variable of index location of first comma in periodIndex list
    var index2 = periodIndex.indexOf(",", index1 + 1); //creates variable for second comma in period index list
    var index3 = periodIndex.indexOf(",", index2 + 1); // creates variable for third comma in period index list
    var first = periodIndex.slice(index1 + 1,index2); //creates numerical value containing first number set in periodIndex
    var second = periodIndex.slice(index2 + 1,index3); // creates numerical value containing second number set in periodIndex
    //alert(periodIndex.slice(index2 + 1,index3));
    //alert(second);
    //alert(periodIndex.length);
    
    var temp = str.substring(first -1,second -1);//creates string containing first sentence that has a period in front of it
    var patt=/arr/g; //used to search for "arr"
    var result=patt.test(temp); //checks if the sentence stored in temp contains the phrase "arr"
     //alert(result);
    if(result == 0) // if there is no "arr" in the temp sentence run random number if loop
    {
    	if(randomNumber >= 5) // if random number is greater than or equal to 5 replaces the period at the beginning of the sentence with ". Arr"
    	{
    	temp = temp.replace(".", ". Arr");
    	}
    }
    str2 += temp //adds the temp data to the end of str2
    periodIndex = periodIndex.slice(index2); //removes the used index values from periodIndex
    
    }
    document.write(periodIndex + "\r" + str + "\r" + randomNumber + "\r" + str2);
    
    </script>
    
    </body>
    </html>
    A programmer started to cuss
    Because getting to sleep was a fuss
    As he lay there in bed
    Looping ‘round in his head
    was: while(!asleep()) sheep++;

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Sorry if I completely missed the point on this one, while re-inventing the universe at the same time.

    Code:
    <script type="text/javascript">
    
    	var
    		str = 'The rain in Spain stays mainly in the plain. I like nachos. I am batman. Well hello there arr.',
    		data, i;
    
    	data = str.split(/\.\s*/g);
    	for (i = 0; i < data.length; i++) {
    		if (data[i] && (data[i].toLowerCase().indexOf('arr') === -1) && (Math.random() > 0.5)) {
    			data[i] = 'Arr ' + data[i];
    		}
    	}
    	document.write(data.join('. '));
    
    </script>
    Script breaks up the string, checks is "arr" exists anywhere in each array member, then theres a 50&#37; chance it prepends "Arr" to the beginning. Sticks it all back together and writes it to the screen.
    Last edited by bionoid; 12-04-2011 at 05:51 PM.

  3. #3
    Join Date
    Nov 2011
    Location
    Corvallis Oregon
    Posts
    12
    Wow that is much simpler, thanks, I was worried I may have been making it too complex...

    for the sake of learning I have a clarifying question

    what does the red text do?

    data = str.split(/\.\s*/g);

    I understand that the /g means global and that the \. is splitting at each period, just unclear about the \s*
    A programmer started to cuss
    Because getting to sleep was a fuss
    As he lay there in bed
    Looping ‘round in his head
    was: while(!asleep()) sheep++;

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    The \s is for white space characters, like spaces, tabs etc.
    * means that it can match zero or more characters (\s characters).
    So im basically just removing any spaces that are following the "."

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

  5. #5
    Join Date
    Nov 2011
    Location
    Corvallis Oregon
    Posts
    12

    Thumbs up

    Ok, I understand now.. thank you so much for your help!
    A programmer started to cuss
    Because getting to sleep was a fuss
    As he lay there in bed
    Looping ‘round in his head
    was: while(!asleep()) sheep++;

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    You're welcome... sheep++ time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles