Javascript- Regular expression

Joe
 sampleText='6501-*-5-[*]'

Expected output

 result='6501-%-5-[*]'

I have to replace * by % and leave [*] as it is. There might be multiple occurance of both. How can we i do it in javascript. Thanks in advance.

frogatto

Try this:

sampleText = sampleText.replace(/\*/g, function(match, offset){
    if(offset > 0) {
        if(sampleText[offset - 1] == '[' && sampleText[offset + 1] == ']'){
            return match;
        }
    }
    return '%';
});

fiddle: here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related