简体   繁体   English

Javascript正则表达式在括号中提取

[英]Javascript regex to extract in brackets

I am working with some html that is created at runtime, where I need to get text using regex in Javascript. 我正在使用在运行时创建的一些html,我需要在Java中使用正则表达式获取文本。 The html has an edit box that contains some text, and an image tag that looks like this: html有一个包含一些文本的编辑框和一个如下所示的图像标签:

<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. 我需要能够提取“ [[image:logo_aa]]”部分,然后用它替换整个html图像标签。 But or some reason, regex only gets "a]" when I run it. 但是或出于某些原因,正则表达式在运行时仅获得“ a]”。 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"); var imageTagPattern = new RegExp(“ [[Image:\\ s \\ S +]]”,“ i”);

Debuggex Demo Debuggex演示

You need to use double \\\\ for RegExp object creation: 您需要使用双\\\\来创建RegExp对象:

var imageTagPattern = new RegExp("\\[\\[Image:\\s\\S+\\]\\]", "i");

Or just use regex literal: 或者只使用正则表达式文字:

var imageTagPattern = /\[\[Image:\s\S+\]\]/i;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM