1

actually I want to replace a string except if it comes in a round bracket.

For example:

JAVA is a nice language. (JAVA) has lots of features. JAVA is portable. (JAVA) even comes in 64 bit.

now if i replace JAVA with C:

then desired output is :

C is a nice language. (JAVA) has lots of features. C is portable. (JAVA) even comes in 64 bit. 

even java can come as a sub string like

 XJAVAX is a nice language. (XJAVAX) has lots of features. JAVA is portable. (JAVA) even comes in 64 bit.

now if i replace JAVA by C :

Then expected output :

XCX is a nice language. (XJAVAX) has lots of features. C is portable. (JAVA) even comes in 64 bit.
3
  • 7
    Please don't replace Java with C, please... Commented Nov 23, 2011 at 9:13
  • 1
    it's just an example..i din't really mean it..I hope u r confused..I would suggest u 2 leave s/w industry and join some ..... Commented Nov 23, 2011 at 9:16
  • I think that those whose upvoted the comment @Mudassir understood the stuff. Commented Nov 23, 2011 at 10:52

1 Answer 1

3

Since JavaScript doesn't have look-behind, the easiest way is probably an alternation:

str = str.replace(/\(JAVA\)|JAVA/g, function(m) {
    return m === "(JAVA)" ? m : "C";
    // Or
    // return m.length === 6 ? m : "C";
});

Beyond that, you get into trying to simulate look-behind, which is possible in some ways, but messy.

Sign up to request clarification or add additional context in comments.

1 Comment

@Neel: That's probably easily addressed by putting a word boundary marker on either side of the alternation: \b\(JAVA\)|JAVA\b. Running out the door so I couldn't test that. The MDC page on RegExp can be a useful guide. The spec has all the details, but it's not as readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.