无效的正则表达式错误 [英] Invalid regular expression error
问题描述
我正在尝试检索此字符串 property_id = 516& category = featured-properties
的类别部分,因此结果应该是特色 - 属性,我想出了一个正则表达式并在这个网站上测试了它 http:// gskinner。 com / RegExr / ,它按预期工作,但是当我将正则表达式添加到我的javascript代码中时,我出现了无效的正则表达式错误,有人能告诉我这段代码搞乱了吗?
I'm trying to retrieve the category part this string "property_id=516&category=featured-properties
", so the result should be "featured-properties", and I came up with a regular expression and tested it on this website http://gskinner.com/RegExr/, and it worked as expected, but when I added the regular expression to my javascript code, I had a "Invalid regular expression" error, can anyone tell me what is messing up this code?
谢谢!
var url = "property_id=516&category=featured-properties"
var urlRE = url.match('(?<=(category=))[a-z-]+');
alert(urlRE[0]);
推荐答案
积极的外观(你的?不符合ECMAScript 2018标准的JavaScript环境不支持< =
),这会导致您的RegEx失败。
Positive lookbehinds (your ?<=
) are not supported in JavaScript environments that do not comply with ECMAScript 2018 standard, which is causing your RegEx to fail.
您可以通过一系列不同的方式模仿它们,但这可能是一个更简单的RegEx来完成工作:
You can mimic them in a whole bunch of different ways, but this might be a simpler RegEx to get the job done for you:
var url = "property_id=516&category=featured-properties"
var urlRE = url.match(/category=([^&]+)/);
// urlRE => ["category=featured-properties","featured-properties"]
// urlRE[1] => "featured-properties"
这是一个非常简单的例子,但是要搜索StackOverflow以解析RegEx模式如果您需要,URL参数将会显示更强大的示例。
That's a super-simple example, but searching StackOverflow for a RegEx pattern to parse URL parameters will turn up more robust examples if you need them.
这篇关于无效的正则表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!