0

I want to redirect certain facebook urls. So far I have a working chrome plugin, but some urls are not matched the right way.

var patternURL = new RegExp("http(s)?://www\.facebook\.com(/|/@?ref=(tn_tnmn|logo)+)?$", "m");

Should match:

https://www.facebook.com/?ref=tn_tnmn
https://www.facebook.com/?ref=logo
https://www.facebook.com/
https://www.facebook.com

But not:

https://www.facebook.com/messages/

It seams in JavaScript the end of string ($) is not recognized correctly. Thats what my jsfiddle demo shows so far. It should be True but is always false even with "m".

What do I do wrong?

3
  • Have you tried the Kleene star (i.e. *)? Commented Jun 14, 2014 at 11:40
  • Like this "http(s)?://www\.facebook\.com(/|/@?ref=*)$"? Yes. Commented Jun 14, 2014 at 11:43
  • maybe because you have the @ in /@? Commented Jun 14, 2014 at 13:16

1 Answer 1

2

The ? is not escaped. You need to use two backslashes to escape it.

patternURL = new RegExp("http(s)?://www\.facebook\.com(/|/\\?ref=(tn_tnmn|logo)+)?$", "m");

http://jsfiddle.net/tRm7U/8/

This is because when you use the regexp constructor, you use a string, and the first backslash is "used" to escape in the string before the regexp object is constructed. You don't need it if you declare it like this:

patternURL = /http(s)?://www\.facebook\.com(/|/\?ref=(tn_tnmn|logo)+)?$/m

Sign up to request clarification or add additional context in comments.

2 Comments

you still need to provide the "/" in front of "\\?"
Thank you! I now use this "^https?://www\\.facebook\\.com(/(\\?.*)?)?$" which uses your idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.