admin管理员组

文章数量:1391974

This is my pattern:

var pattern = "/(?:https?:\/\/)?(?:www\.)?facebook\\/(?:(?:\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/(?:(?: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.

This is my pattern:

var pattern = "/(?:https?:\/\/)?(?:www\.)?facebook\.\/(?:(?:\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./(?:(?: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.

Share Improve this question edited Mar 6, 2014 at 10:05 Kid Diamond asked Mar 6, 2014 at 9:59 Kid DiamondKid Diamond 2,3018 gold badges40 silver badges85 bronze badges 1
  • how are you using this in your javascript? – SajithNair Commented Mar 6, 2014 at 10:04
Add a ment  | 

3 Answers 3

Reset to default 6

Remove unwanted double quotes from regex pattern:

 var pattern = /(?:https?:\/\/)?(?:www\.)?facebook\.\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-\.]*)/;

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

var pattern = "/(?:https?:\\/\\/)?(?:www\\.)?facebook\\.\\/(?:(?:\\w)*#!\\/)?(?:pages\\/)?(?:[\\w\\-]*\\/)*([\\w\\-\\.]*)/";

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

why not use split instead ?

something like this

var str = "http://www.facebook./xyz";
var res = str.split('/').pop();
console.log(res);

本文标签: javascriptRegExp range out of order with complex patternStack Overflow