helmet91
helmet91

Reputation: 127

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.

RegExp: /\[img=?.*?http:\/\/lorempixel\.com\/640\/480.*?\/img\]/

String to match: [img=http://lorempixel.com/640/480]description[/img]

Script: JSFiddle

Can anyone find the problem here?

Upvotes: 0

Views: 1759

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191829

You can't concatenate a regex using a regex literal, and you need a regex for the .match method.

var regex = new RegExp("\\[img=?.*?" + regexSafeUrl + ".*?\/img\\]");

http://jsfiddle.net/wue82/1/

EDIT: James Montague is correct that .match will implicitly use a RegExp. You don't need the / in the string that gets converted to RegExp though or these will be considered literal slashes. Your real problem was doing this as well as not properly escaping in the string.

http://jsfiddle.net/wue82/3/

Upvotes: 2

Related Questions