JavaScript 正则表达式(缺少“/”),我该如何解决这个问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10471561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript regex (missing '/'), how do I fix this?
提问by Stan
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?
有人可以看看我的正则表达式代码中的问题在哪里吗?
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/
回答by drinchev
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/
回答by Tobias Krogh
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:更新的小提琴只提醒图像列表
回答by SLaks
You need to escape the /
in the middle of the regex by eriting \/
.
您需要/
通过 eriting来避开正则表达式中间的\/
。