开发者

Help with Regex expression required

I am hoping someone can help me find a suitable regex expression in javascript that meets the requirement of the statement below. I have tried a few expressions but including an asterisk character as a character literal in the search complicates things.

Given string str, match and replace all occurences of an asterisk cha开发者_如何学Pythonracter with the correct escape sequence to treat it as a character literal, but ignore any asterisk character contained in a sequence like this in the string: <*>

Any help, much appreciated. Danie


    var x='xx**<*>*><*<*>>>*';
    x=x.replace(/(<)?\*(>)?/g, function($0, $1 ,$2){

        return $1 ? $2 ? $0 : 'y' : $2 ? 'y' : 'y';
});
alert(x);

http://jsfiddle.net/yDT25/


If it's exactly the <*> sequence that you have to avoid replacing * in (and you are using PCRE or the like) then you can use something like: /(?<!<)\*(?!>)/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜