简体   繁体   English

JavaScript正则表达式(缺少'/'),我该如何解决这个问题?

[英]JavaScript regex (missing '/'), how do I fix this?

I have simple script, I want to get my images and then divide them by commas. 我有简单的脚本,我想获取我的图像,然后用逗号分隔它们。 However I cannot get image links inside [gallery] tags since I get Uncaught SyntaxError: Invalid regular expression: missing / . 但是我无法获取[gallery]标签内的图像链接,因为我得到Uncaught SyntaxError: Invalid regular expression: missing /

Can someone please take a look and show me where is the problem in my RegEx code? 有人可以看看,并告诉我我的RegEx代码中的问题在哪里?

HTML HTML

<textarea>
abc
[gallery]/content/img/upload/img-2012.03.19.-634677727044317051.jpg, /content/img/upload/img-2012.03.19.-634677727046997204.jpg, /content/img/upload/img-2012.03.19.-634677727049487347.jpg, /content/img/upload/img-2012.03.19.-634677727051787478.jpg, /content/img/upload/img-2012.03.19.-634677727054137613.jpg[/gallery]
def
</textarea>​

JavaScript JavaScript的

$(function(){
    var text = $('textarea').val();
    var re = /\[gallery\]([^}]+)[/gallery\]/g;
    var gallery = re.exec(text);
    alert(gallery);
});​

Fiddle : http://jsfiddle.net/BcFsg/ 小提琴http//jsfiddle.net/BcFsg/

yep, the problem is you missed one escape character 是的,问题是你错过了一个逃脱角色

var re = /\[gallery\]([^}]+)\[\/gallery\]/g;
//                          |
//                       [ HERE ]

jsfiddle here http://jsfiddle.net/BcFsg/1/ jsfiddle这里http://jsfiddle.net/BcFsg/​​1/

你需要躲避/由eriting在正则表达式的中间\\/

see the updated fiddle... I do not know why but it seems you do not have to escape the closing ] 看到更新的小提琴...我不知道为什么,但似乎你不必逃避关闭]

EDIT: gave wrong output (all squared brackets need to be escaped)... updated fiddle EDIT 2: updated fiddle to only alert the list of images 编辑:输出错误(所有方括号都需要转义)...更新小提琴编辑2:更新小提琴只提醒图像列表

fiddle 小提琴

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

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