JavaScript Regex:在匹配和另一个char

时间:2015-09-02 09:56:03

标签: javascript regex wordpress

我遇到以下文字和正则表达式方法的问题。我从我的服务器(从Wordpress数据库)检索文本,我想用Regex从中提取图像src

来自服务器的字符串如下所示:

...
[other_directives ...]
[et_pb_image admin_label="Bild" 
    src="http://url.com/wp-content/uploads/2015/08/imageXYZ.jpg"
     show_in_lightbox="off" url_new_window="off" animation="left" sticky="off" align="left" 
    force_fullwidth="off" always_center_on_mobile="on" use_border_color="off" 
    border_color="#ffffff" border_style="solid" alt="some text"]
[other_directives ...]
...

我想搜索et_pb_image字符串,并希望在其中的src文字的撇号之间提取文字。

纯正的Regex可以实现吗?

修改

到目前为止我尝试过(我是一名正则表达式的初学者):

/(et_pb_image)?(src=").+[a-z]/

这将返回src,但带有src="..."标记。

2 个答案:

答案 0 :(得分:1)

你需要非常小心正则表达式解析这些文本。几乎每次我们都要承担一些事情。因此,在这种情况下,我们假设您在]et_pb_image属性之间不会有src。此外,我们假设src属性值包含"

然后,您可以使用

var re = /et_pb_image[^\]]*?src="([^"]+)"/ig; 
var str = '...\n[other_directives ...]\n[et_pb_image admin_label="Bild" \n     show_in_lightbox="off" url_new_window="off" animation="left" sticky="off" align="left" \n    force_fullwidth="off" always_center_on_mobile="on" use_border_color="off" \n    src="http://url.com/wp-content/uploads/2015/08/imageXYZ.jpg"\n[other_directives ...]\n...\n\n...\n[other_directives ...]\n[et_pb_image admin_label="Bild" \n    src="http://url.com/wp-content/uploads/2015/08/imageXYZ.jpg" border_color="#ffffff" border_style="solid" alt="some text"]\n     show_in_lightbox="off" url_new_window="off" animation="left" sticky="off" align="left" \n    force_fullwidth="off" always_center_on_mobile="on" use_border_color="off" \n    \n    border_color="#ffffff" border_style="solid" alt="some text"]\n[other_directives ...]\n...\n...\n[other_directives ...]\n[et_pb_image admin_label="Bild" \n    src="http://url.com/wp-content/uploads/2015/08/imageXYZ.jpg"\n     show_in_lightbox="off" url_new_window="off" animation="left" sticky="off" align="left" \n    force_fullwidth="off" always_center_on_mobile="on" use_border_color="off" \n    border_color="#ffffff" border_style="solid" alt="some text"]\n[other_directives ...]';
var m;
 
while ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    document.write(m[1] + "<br/>");
}

正则表达式/et_pb_image[^\]]*?src="([^"]+)"/ig匹配

  • et_pb_image - 文字et_pb_image
  • [^\]]*? - ]以外的任何字符,尽可能少
  • src=" - 文字src="
  • ([^"]+) - 除"以外的1个或多个字符(假设src属性值用双引号括起来总是
  • " - 文字"

我们需要在所有匹配中捕获第1组,而使用string.match无法实现,我们必须使用exec

答案 1 :(得分:0)

使用javascript:

myLongString.match( /et_pb_image.+\s+src="(.+)"/g )

Regular expression visualization

Debuggex Demo