如何在ruby中获取第一个html标记和第二个html标记之间的内容(how to get the content between the first html tag and the second html tag in ruby)

嗨朋友〜
我想获取第一个html标记和第二个html标记之间的内容。
例如<p>Hello <bold>world</bold>!</p>将返回Hello

我应该在Ruby中做什么?
谢谢〜


Hi friend~
I want get the content between the first html tag and the second html tag.
for example <p>Hello <bold>world</bold>!</p> will return Hello

What should I do in Ruby?
Thank you~

2022-12-04 17:12

满意答案

正则表达式将是: <[^>]*>([^<]*)

  1. <[^>]*> - 数学thiw第一个标签“<...>”
  2. ([^<]*) - 捕获文本以打开下一个标记“<...> 一些文本 <...>”

如何将他应用于Rubby - 我不知道

http://www.regular-expressions.info/ruby.html


Regex will be: <[^>]*>([^<]*)

  1. <[^>]*> - math thiw first tag "<...>"
  2. ([^<]*) - capture text to open next tag "<...> some text <...>"

how apply him on Rubby - i dont know

look http://www.regular-expressions.info/ruby.html

相关问答

更多

为什么我应该使用content_tag而不是“常规”html?(why should I use content_tag instead of “regular” html?)

能够使用content_tag有助于在Ruby代码中编程生成HTML,例如:助手和演示者。 在我看来,我通常不会在项目的布局和模板中使用content_tags - 我认为它不会帮助编码的可读性。 在这里,我看不到任何生产力或表现的收益。 但是:建议的意思:如果这就是你的团队已经标准化的东西 - 与团队一起去。 Being able to use the content_tag helps when you are programmatically generating HTML inside o...

在第一个标记之前获取HTML(Getting HTML before first
tag)

$content = "this is my content <br /> as it continues <br /> another break tag"; $parts = explode("<br />", $content); $first = trim($parts[0]); 爆炸功能: http : //php.net/manual/en/function.explode.php 按字符串拆分字符串 修剪功能: http : //www.php.net/manual/en/func...

如何在元标记中使用html_safe:在rails上的ruby中的描述内容(how to use html_safe in meta tag og:description content in ruby on rails)

我在我的PHP代码中遇到此错误以显示og文件。 我想,它会在轨道上工作。 你只需将""改成'' <meta property="og:description" content='<%= @post.description.html_safe %>'/> 要么 <meta property="og:description" content='<%= @post.description %>'/> 我希望这对你有帮助。 I have ever got this error in my php co...

如何在Ruby中从HTML文件中提取