I am trying to clean an URL (rss feed) such that after the last .rss (or.html) there are no further characters. I’m using the TryIt Editor on w3schools.com for testing. The following is my test code:
var str="http://rss.cnn.com/rss/cnn_world.rsstest";
var patt1=/(.*[.rss|.html]).*/g;
var result = str.replace(patt1, "$1");
document.write(result);
The problem I am having is that the result shown is
http://rss.cnn.com/rss/cnn_world.rsstest
i.e. the “test” didn’t get removed. I am wondering if someone could check my regex and explain what I am doing wrong?
Thank you.
Firstly, I recommend jsFiddle or some other testing service. Forgive my bias.
Some other answerers seem to have completely missed the point, so to explain your errors:
[]does not group—it defines a character class. What you’ve written actually matches a single character, namely any of these:.|hlmrst.$anchor the two.*s may not match what you’d expect.Try instead:
Here’s the jsFiddle demo.