繁体   English   中英

JavaScript Regex:获取匹配项和另一个字符之间的字符串

[英]JavaScript Regex: Get string between a match and another char

我对以下文本和 Regex 方法有疑问。 我从我的服务器(从 Wordpress 数据库)检索文本,我想用正则表达式从中提取图像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文本的撇号之间的文本。

这可以用纯正则表达式吗?

编辑

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

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

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

您需要非常小心地使用正则表达式解析此类文本。 几乎每次我们都必须假设一些东西。 因此,在这种情况下,我们假设在et_pb_imagesrc属性之间没有] 此外,我们假设 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

使用javascript:

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

正则表达式可视化

调试器演示

暂无
暂无

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

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