2

This is my pattern:

var pattern = "/(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-\.]*)/";
var matches = $("#search input").val().match(new RegExp(pattern));

When I use it, it gives me an error:

Uncaught SyntaxError: Invalid regular expression: //(?:https?://)?(?:www.)?facebook.com/(?:(?:w)*#!/)?(?:pages/)?(?:[w-]*/)*([w-.]*)//: Range out of order in character class

From reading on another similar issues it came to my attention that I need to double escape some characters, but I don't know which out of all from my pattern.

Kid Diamond
  • 2,232
  • 8
  • 37
  • 79

3 Answers3

6

Remove unwanted double quotes from regex pattern:

 var pattern = /(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-\.]*)/;
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
0

In a JavaScript String all backslashes should be replaced by double backslash

var pattern = "/(?:https?:\\/\\/)?(?:www\\.)?facebook\\.com\\/(?:(?:\\w)*#!\\/)?(?:pages\\/)?(?:[\\w\\-]*\\/)*([\\w\\-\\.]*)/";
SajithNair
  • 3,867
  • 1
  • 17
  • 23
0

if its just about getting the xyz part from the url http://www.facebook.com/xyz

why not use split instead ?

something like this

var str = "http://www.facebook.com/xyz";
var res = str.split('/').pop();
console.log(res);
aelor
  • 10,892
  • 3
  • 32
  • 48