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
3 Answers
Reset to default 6Remove 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条)