RegExp范围无序,复杂模式 [英] RegExp range out of order with complex pattern

查看:112
本文介绍了RegExp范围无序,复杂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模式:

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.

推荐答案

从正则表达式模式中删除不需要的双引号:

Remove unwanted double quotes from regex pattern:

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

这篇关于RegExp范围无序,复杂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆