javascript - RegExp range out of order with complex pattern - Stack Overflow

This is my pattern:var pattern = "(?:https?:)?(?:www.)?facebook\(?:(?:w)*#!)?(?:pages

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744712822a4589439.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信