Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多行上的jQuery正则表达式_Jquery_Regex_Line Breaks - Fatal编程技术网

多行上的jQuery正则表达式

多行上的jQuery正则表达式,jquery,regex,line-breaks,Jquery,Regex,Line Breaks,我有以下字符串: <img alt="over 40 world famous brandedWATCHES BRANDs to choose from " src="http://www.fastblings.com/images/logo.jpg"></strong></a><br> 我想定义一个正则表达式模式,比如,但是正如您所看到的,目标字符串在alt属性中有一个换行符-那么我如何合并它呢?该规则应类似于“alt属性中的任何内容,包括

我有以下字符串:

<img alt="over 40 world famous brandedWATCHES BRANDs to choose from
" src="http://www.fastblings.com/images/logo.jpg"></strong></a><br>


我想定义一个正则表达式模式,比如
,但是正如您所看到的,目标字符串在alt属性中有一个换行符-那么我如何合并它呢?该规则应类似于“alt属性中的任何内容,包括换行符”。

默认情况下,
通配符运算符与换行符不匹配(
\n
\r
)。在其他语言中,有一种
DOTALL
模式(有时称为单行模式)使
匹配任何内容。Javascript由于某种原因没有它。如果要使用等效字符,请使用
[\s\s]
,这意味着任何字符都是空白或不是空白,因此:

/<img alt="([\s\S]+?)" src="http:\/\/(.+?)\.(jpg|gif)">/
或者,如果希望存在
alt
属性:

$("img[alt][src='http://.*\.gif|jpg']").each(function() {
  var alt = $(this).attr("alt");
  var src = $(this).attr("src");
  ...
});

不要使用正则表达式解析HTML!啊,
问题
标签。如果我们得到一些不是问题的问题…你选择的答案实际上是不正确的。查看我的答案。删除我的评论,因为我认为它仍然有效,因为接受的答案使用相同的方法。无论如何,很高兴听到我没有错过一些明显的要点;)顺便说一句,我给你加1分。
$("img[alt][src='http://.*\.gif|jpg']").each(function() {
  var alt = $(this).attr("alt");
  var src = $(this).attr("src");
  ...
});