Reputation: 13
I am working with some html that is created at runtime, where I need to get text using regex in Javascript. The html has an edit box that contains some text, and an image tag that looks like this:
<img src="http://server/directory/subdirectory/Images1/logo_aa.png" alt=" logo_ce" data-tag=" [[image: logo_aa]] ">
I need to be able to extract the "[[image: logo_aa]]" portion out, and then replace the whole html image tag with it. But or some reason, regex only gets "a]" when I run it. Here is an example of the code that I am using:
var imgHtmlPattern = new RegExp("<img[^>]*?>", "g");
var imageTagPattern = new RegExp("\[\[Image:\s\S+\]\]", "i");
var str = 'A whole paragraph of text here. <img src="http://server/directory/subdirectory/Images1/logo_aa.png" alt=" logo_ce" data-tag=" [[image: logo_aa]] ">';
var matches = str.match(imgHtmlPattern);
for (i = 0; i < matches.length; i++) {
var test = imageTagPattern.exec(matches[i]);
alert(test);
// var tag = matches[i].match(imageTagPattern);
// alert(tag);
}
var imageTagPattern = new RegExp("[[Image:\s\S+]]", "i");
Upvotes: 1
Views: 54
Reputation: 784958
You need to use double \\
for RegExp
object creation:
var imageTagPattern = new RegExp("\\[\\[Image:\\s\\S+\\]\\]", "i");
Or just use regex literal:
var imageTagPattern = /\[\[Image:\s\S+\]\]/i;
Upvotes: 1