Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Ruby on rails 有条件地剥离HTML节点-Regexp/gsub_Ruby On Rails_Regex - Fatal编程技术网

Ruby on rails 有条件地剥离HTML节点-Regexp/gsub

Ruby on rails 有条件地剥离HTML节点-Regexp/gsub,ruby-on-rails,regex,Ruby On Rails,Regex,我想通过删除某些html节点(包括子节点)(尤其是标题和图像)并删除所有其他标记(例如段落),同时保留子节点,来生成文章的搜索预览 e、 g 然而我需要 Subject is the who, what, where, why and when. 我正在使用Rails插件清理用户输入,这非常有效;事实上,我可以定义一个洗涤器来实现这一点,但是对于这个简单的操作来说,一个regexp似乎就足够了 提前感谢您的建议。使用几个正则表达式: "<h2>Subject</h2>&

我想通过删除某些html节点(包括子节点)(尤其是标题和图像)并删除所有其他标记(例如段落),同时保留子节点,来生成文章的搜索预览

e、 g

然而我需要

Subject is the who, what, where, why and when.
我正在使用Rails插件清理用户输入,这非常有效;事实上,我可以定义一个洗涤器来实现这一点,但是对于这个简单的操作来说,一个regexp似乎就足够了

提前感谢您的建议。

使用几个正则表达式:

"<h2>Subject</h2><p>Subject is the who, what, where, why and when.</p>".
    gsub(/<h\d>[^>]*>/,'').
    gsub(/<img[^>]*>/,'').
    gsub(/<\/?[^>]*>/, '')
“主题主题是谁、什么、在哪里、为什么和什么时候。

”。 gsub(/[^>]*>/,'')。 gsub(/]*>/,'')。 gsub(/]*>/,'')
然而,应该注意的是,您已经达到了regexp在处理html时所能处理的复杂性极限。如果您需要执行更复杂的操作(如基于类名删除等),那么您应该真正使用html解析器。

尝试:

myline = line.gsub!(/(<[^>]*>)|\n|\t/s) {" "}
myline=line.gsub!(/(]*>)|\n |\t/s){“}

谢谢!尽管我们认为一个regexp是可能的,但这非常有效。正如你所说,我想我可以用丝瓜。
"<h2>Subject</h2><p>Subject is the who, what, where, why and when.</p>".
    gsub(/<h\d>[^>]*>/,'').
    gsub(/<img[^>]*>/,'').
    gsub(/<\/?[^>]*>/, '')
myline = line.gsub!(/(<[^>]*>)|\n|\t/s) {" "}