6 Replies - 1194 Views - Last Post: 17 September 2011 - 09:40 PM

#1 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Regex for selecting specific URLs

Posted 16 September 2011 - 01:59 PM

Hi guys,

JS isn't my usual language, but I'm venturing out to write a Chrome extension. This extension basically just injects a script on a page, so it behaves just like any other JS.

Anyway, the script finds specific URLs on a specific page, and changes them slightly. I'm using regex to find these URLs. I've got one working, but I need another, and I can't figure it out.

The first is this:

"http:\\/\\/www.reddit.com\\/r\\/\\S*\\/comments\\/\\S*\\/$)"


Also, my skills with regex are miserable, so if you know of any way to make this better, I'm all ears.

This matches URLs like this:

http://www.reddit.com/r/gaming/comments/khsha/wtf_is_a_katamari/
http://www.reddit.com/r/anything/comments/hash/anything/


Now I'm trying to match URLs like this:

http://www.reddit.com/r/gaming/comments/khsha/wtf_is_a_katamari/c2kck9i
http://www.reddit.com/r/anything/comments/hash/anything/anotherHash


If you know how to modify the original regex so that it'll match the second kind of URL (but not the first), or some way to make them both better, please let me know.

Thanks for reading.

Is This A Good Question/Topic? 0
  • +

Replies To: Regex for selecting specific URLs

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: Regex for selecting specific URLs

Posted 16 September 2011 - 02:14 PM

Yeah regex blows..

Try this:
http:\/\/www.reddit.com\/r\/.*\/comments\/.*


Sites I have book marked.
http://www.regextester.com/
http://regexlib.com/...CookieSupport=1
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Regex for selecting specific URLs

Posted 16 September 2011 - 02:22 PM

I think that regex as a concept is awesome. Learning and writing regex certainly blows for sure.

That is a better regex than the one I was using, but unfortunately, it matches both sets of URLs, the one with the ending hash, and the one without.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: Regex for selecting specific URLs

Posted 16 September 2011 - 02:25 PM

Aaaaah.. okay. I was tripped up on the "match the second but not the first".
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Regex for selecting specific URLs

Posted 16 September 2011 - 02:28 PM

I'm working on figuring it out. It's probably going to have something to do with a $ at the end.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: Regex for selecting specific URLs

Posted 16 September 2011 - 02:29 PM

When the first is a subset of the second that sucks. Hmm.. maybe keep track of left slashes?
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Regex for selecting specific URLs

Posted 17 September 2011 - 09:40 PM

It really depends on what's at the end of the URL. With webpages it's usually a double quote (could be a single quote also).
<a href="http://www.webpage.com">Web page</a>


Using $ would mean that the your match would have to be directly at the end of the entire string which is not the case in a web page.

Check these two expressions out.
var without_hash_exp = /http:\/\/www.reddit.com\/r\/([^\/].*)\/comments\/([^\/].*)\/([^\/].*)\/"/
var with_hash_exp = /http:\/\/www.reddit.com\/r\/([^\/].*)\/comments\/([^\/].*)\/([^\/].*)\/([^\/].*)"/

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1