/    Sign up×
Community /Pin to ProfileBookmark

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(/bfaadab/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 🙂

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Aug 30.2009 — [url=http://www.javascriptkit.com/jsref/regexp.shtml]Lookahead[/url].

/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.
Copy linkTweet thisAlerts:
@pelconesauthorAug 30.2009 — Thanks a million. Very fast and accurate answer
Copy linkTweet thisAlerts:
@pelconesauthorAug 30.2009 — 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.
×

Success!

Help @pelcones spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...