dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Replacing letter combinations with exceptions

  1. #1
    Join Date
    Aug 2009
    Posts
    20

    Replacing letter combinations with exceptions

    I have a textarea where one can enter text (inputtext). Once the text is entered one can press a button and certain characters and words will be replaced in another textarea (outputtext). I want to do this: When the word faada (for example) shows up it will after the button is pressed read faada but when text such as faa or any other word containing aa (but not the word faada, the exception) is entered it will generate tt instead of the aa

    For example: faa raa gaa faada haarry faada
    After button is pressed: ftt rtt gtt faada httrry faada

    I have tried

    function addtext() {
    var newtext = document.myform.inputtext.value;
    newtext2=newtext.replace(/\bfaada\b/g, "faada");
    newtext3=newtext2.replace(/aa/g, "tt");
    document.myform.outputtext.value = newtext3;
    }

    Problem is that since aa is global (which I guess it has to be) the aa in faada will also be replaced with tt while I want that specific word to stay the same.

    Can anyone help me :-)

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Lookahead.
    /faa(?=da)/ Matches faa followed by da, but will only replace the faa.
    /faa(?!da)/ Matches faa not followed by da, but only replaces the faa.

  3. #3
    Join Date
    Aug 2009
    Posts
    20

    Smile

    Thanks a million. Very fast and accurate answer

  4. #4
    Join Date
    Aug 2009
    Posts
    20
    I just realized a little obsticle

    I have now done the following function

    function addtext() {
    var newtext = document.myform.inputtext.value;
    newtext2=newtext.replace(/faa(?=da)/g, "faa");
    newtext3=newtext2.replace(/aa(?!da)/g, "tt");
    document.myform.outputtext.value = newtext3;
    }

    Only problem is that I want aada to be ttda. With this code it keeps it aada intact.

    faada aada raada faa aa should generate:

    faada ttda rttda ftt tt

    The solution might be simple but I cannot see it. Thanks again for all the help.

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