[英]Javascript regex doesn't match
I have been stucked with this problem for hours. 我已经被这个问题困扰了几个小时。 I have a regex pattern and a matching string.
我有一个正则表达式模式和一个匹配的字符串。 Regarding to regex101.com , it is sure a matching string, but in my script, regarding to JSFiddle , it doesn't match.
关于regex101.com ,肯定是匹配的字符串,但是在我的脚本中,关于JSFiddle ,它不匹配。
RegExp: /\\[img=?.*?http:\\/\\/lorempixel\\.com\\/640\\/480.*?\\/img\\]/
RegExp:/
/\\[img=?.*?http:\\/\\/lorempixel\\.com\\/640\\/480.*?\\/img\\]/
: /\\[img=?.*?http:\\/\\/lorempixel\\.com\\/640\\/480.*?\\/img\\]/
String to match: [img=http://lorempixel.com/640/480]description[/img]
要匹配的字符串:
[img=http://lorempixel.com/640/480]description[/img]
Can anyone find the problem here? 有人可以在这里找到问题吗?
You can't concatenate a regex using a regex literal, and you need a regex for the .match
method. 您不能使用正则表达式文字连接正则表达式,并且
.match
方法需要一个正则表达式。
var regex = new RegExp("\\[img=?.*?" + regexSafeUrl + ".*?\/img\\]");
http://jsfiddle.net/wue82/1/ http://jsfiddle.net/wue82/1/
EDIT: James Montague is correct that .match
will implicitly use a RegExp. 编辑:James Montague是正确的
.match
将隐式使用RegExp。 You don't need the /
in the string that gets converted to RegExp though or these will be considered literal slashes. 不过,您不需要在转换为RegExp的字符串中使用
/
,否则它们将被视为文字斜杠。 Your real problem was doing this as well as not properly escaping in the string. 您真正的问题是这样做以及没有正确转义字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.