开发者

a simple regexp

The regexp has to remove ,id or id, in read and unread.

GM_setValue('unreadids', unreadids.replace(new RegExp('('+id+',)|(,'+id+')', 'i'),''));
GM_setValue('readids', readids.replace(new RegExp('('+id+',)|(,'+id+')', 'i'), ''));

In RX To开发者_Python百科olkit, (,1|1,) for a test, ,1 or ,1 is replaced with nothing.

In real life...

http://userscripts.org/scripts/show/73562

http://userscripts.org/scripts/review/

...id isn't replaced with nothing.

Why?


Your question is kind of hard to understand, I don't think it's clearly explaining what you need to do and what is wrong. I'm not sure if the links clarify as they are broken.

I think I see what you mean, but I may be wrong.

If you have a string like

var test = "(674,)|(,674)"

Where 674 is the "id", and you are trying to replace "674," and ",674" with "" to get "()|()"

Then your code should work fine excepting you need the "g" (global) flag as well to replace ALL instances.

Example:

                    var test = "(674,)|(,674)";
                    var id = "674";
                    var expr = new RegExp('(' + id + ',)|(,' + id + ')', 'gi');
                    var b = test.replace(expr, '');
                    alert(test);
                    alert(b);

You're using the same regular expression twice, so why not just save it?

If you're wondering why it doesn't replace instances of JUST "674" in addition to "674," or ",675"...well that's because you didn't write a regular expression to replace it! Yours only covers it with the commas.

If that IS what you are trying to do, replace ",674" and "674," and just plain "674" with null then here is a regex that will match all of those:

var expr = new RegExp(',?' + id + ',?', 'gi');

? matches the preceding character 0 or 1 times.

If you need to match only instances that occure withing ()|() it's pretty easy to adjust the preceding regex for that, I'll leave it as an exercise for the reader.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜