简体   繁体   English

无法正确获取js正则表达式

[英]Cant get js regular expression correct

I am trying to get a regular expression to break a string up in to two matches. 我正在尝试获取正则表达式以将字符串分解为两个匹配项。 here is what I've got so far: https://jsfiddle.net/9rjzttc2/154/ 这是到目前为止我得到的: https : //jsfiddle.net/9rjzttc2/154/

If I remove one of the [img url="http://www.technomag.co.zw/wp-content/uploads/2013/01/quiz.jpg"][/img] it seems to work. 如果我删除[img url="http://www.technomag.co.zw/wp-content/uploads/2013/01/quiz.jpg"][/img]则它似乎可以工作。 The gist is that it should read the string, then replace the non html markup with HTML markup, so that each [img/] tag becomes an html <img/> 要点是,它应该读取字符串,然后将非html标记替换为HTML标记,以便每个[img/]标签都变成一个html <img/>

You are missing a ? 您错过了? , your capture group for the "url" attribute is too greedy (the .+ is consuming more than you expect it to): ,则“ url”属性的捕获组过于贪婪( .+的消耗量超出了您的预期):

/\[(img)\s+(url="(.+?)")\](\w*)\[\/(img)\]/g
//                  ^ this is needed

Yours: https://regex101.com/r/IcKhYK/2 您的: https : //regex101.com/r/IcKhYK/2

This: https://regex101.com/r/IcKhYK/1 这: https : //regex101.com/r/IcKhYK/1

Working jsFiddle . 工作jsFiddle

I updated your regex to the following: 我将您的正则表达式更新为以下内容:

var imgMarkup = /\[(img)\s+(url="(.+?)")\](\w*)\[\/(img)\]*/g;

It appears to produce what you want? 它似乎产生了您想要的? Respond if this is still not accurate. 如果仍然不正确,请回应。

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

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