nginx 反代模块 sub_filter 如何替换单引号

2016-02-19 00:27:31 +08:00
 fantasts

心好累,写出来不是通不过 nginx 的检测就是没效果。
我想替换的内容里带有单引号',例如:'<img src=http'
我先这么尝试 subfilter '<img src=http' ''<img src=https'' //错误<
尝试改成正则 sub_filter '(.)img src=http' ''$1img src=https'’ r; //错误$
_(:з」∠)
不是支持正则么为啥$都错误了

8332 次点击
所在节点    NGINX
13 条回复
rrfeng
2016-02-19 00:42:19 +08:00
知道单引号有问题为撒用正则屏蔽了 < ……
rrfeng
2016-02-19 00:43:30 +08:00
哪里有单引号了……
xuhaoyangx
2016-02-19 00:46:12 +08:00
还有其他一些在后边章节中有特殊用处的标点符号,在前面加 "\" 后,就代表该符号本身。比如:^, $ 都有特殊意义,如果要想匹配字符串中 "^" 和 "$" 字符,则表达式就需要写成 "\^" 和 "\$"。
来自复制粘贴
fantasts
2016-02-19 02:26:43 +08:00
@rrfeng
@xuhaoyangx
就算想植入一段统计代码都不知如何是好
fantasts
2016-02-19 02:32:21 +08:00
@fantasts 大概有点明白了,大概解决了。
demo
2016-02-19 09:45:58 +08:00
\'
popok
2016-02-19 09:48:56 +08:00
@xuhaoyangx 这叫转义
然而正则里面单引号是不需要转义的。

还有 LZ 发帖把问题说清楚点
xuhaoyangx
2016-02-19 11:36:32 +08:00
@popok 我知道转义
fantasts
2016-02-19 16:25:11 +08:00
@popok
不知道咋整
例如:
sub_filter '<div.*<div.*<iframe.*</iframe></div><div class="content">' '<div class="content">' r;
报错:
nginx: [emerg] invalid number of arguments in "sub_filter" directive in

如何才能正确的使用正则而不报错呢。。。
fantasts
2016-02-19 17:28:01 +08:00
@rrfeng
@xuhaoyangx
@demo
@popok
原来 sub_filter 不能使用正则替换,支持正则的是另外一个模块 subs_filter
xuhaoyangx
2016-02-19 17:55:26 +08:00
@fantasts 那個需要自己 add module
jalena
2016-02-19 19:27:08 +08:00
用转义符转义啊 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
liuyinltemp
2018-05-09 13:18:58 +08:00
@fantasts 这个正则怎么写?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/257532

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX