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 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    adding a word boundary in a find word code

    Hi All,

    I am trying to apply a word boundary so that only a complete word is found and changed. In example below the word "walk" is to be changed but not "walking".

    Also is there a need for something else if for instance the final word 'walk' which was being changed had a period such as.... walk. or other character ending such as...... walk! so that the word walk would still be changed.

    I am using the input fields as shown so as to show what is happening.

    Code:
    <input id="str" size=52 value="I went walking along the walk">
    <BR>
    
    <input id="findthis" value="walk">
    <BR>
    
    <input id="replacewith" value="road">
    <BR>
    
    <input id="result" size=52 value="">
    <BR>
    
    <script>
    
    var s=document.getElementById("str").value;
    
    var ft=document.getElementById("findthis").value;
    
    var rw=document.getElementById("replacewith").value;
    
    
    // I do not know how to apply the word boundary shown below
    //whatshouldthisbe = new Regexp("\\b" + whatshouldthisbe + "\\b", "gi" );
    
    
    s=s.replace(ft , rw);
    
    document.getElementById("result").value=s
    
    </script>

    Martin.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Quote Originally Posted by SpidersWebHelp View Post
    I am trying to apply a word boundary so that only a complete word is found and changed. In example below the word "walk" is to be changed but not "walking".
    Update the line assigning the value to ft as follows:

    var ft= new Regexp("\\b"+document.getElementById("findthis").value+ "\\b", "gi" );
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Stephen,

    Do you mean to change as shown in the code below, as it does not seem to function.

    I have tried various ways like you suggested but the same result is they all fail to show anything in the 'result' field.

    Code:
    <input id="str" size=52 value="I went walking along the walk">
    <BR>
    
    <input id="findthis" value="walk">
    <BR>
    
    <input id="replacewith" value="road">
    <BR>
    
    <input id="result" size=52 value="">
    <BR>
    
    <script>
    
    var s=document.getElementById("str").value;
    
    // old changed.. var ft=document.getElementById("findthis").value;
    // new suggestion below
    var ft= new Regexp("\\b" + document.getElementById("findthis").value+ "\\b", "gi" ); 
    
    var rw=document.getElementById("replacewith").value;
    
    s=s.replace(ft , rw);
    
    document.getElementById("result").value=s
    
    </script>

    Martin.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Typo in RegExp - the E needs to be uppercase then it will work.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello Stephen,

    Thank you for the correction, it also explains why some code I have tried on this and other things has previously failed. I kept writing Regexp instead of the correct RegExp but of course never realised I was doing it wrong.

    Martin.


  •  

    Posting Permissions

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