【问题标题】:Can someone help me to find the regular expression for this using javascript有人可以帮我找到这个使用javascript的正则表达式吗
【发布时间】:2012-04-14 14:11:37
【问题描述】:

我需要从下面的表达式中检索 71drwec4 , 51drdsf3"a_secret"></div> 之间的71drwec3 以外的任何其他字符或类似的字符:

<div class="fs" id="a_secret">71drwec4</div>

<div class="fs" id="a_secre">51drdsf3</div>

<div class="fs" id="a_secr">54451drwec3</div>

【问题讨论】:

  • 到目前为止你有没有尝试过?
  • 如果你想对其进行正则表达式,你需要一个正则公式,换句话说,你需要确切地知道表达式每次应该检查什么
  • @colin : 我真的很擅长正则表达式...我试过这个 patt1 = /a_secret\/(\d+)"/;
  • 顺便说一句,一个页面上不应有多个具有相同 ID 的元素
  • 您能简单地找到所有带有 class="fs" 的 div,然后遍历它们并获取文本吗?

标签: javascript regex


【解决方案1】:

您必须为此使用正则表达式吗?如果要解析的文本是 HTML 并且您可以使用 jQuery,则可以使用此处的 .text() 方法通过 ID 检索内容。

例如

var whatIwant = $("#a_secret").text();

http://api.jquery.com/text/

【讨论】:

  • 它可能由 jquert..但我需要正则表达式。谢谢
  • 正则表达式的原因是什么?你能提供更多关于你需要做什么的背景吗?也许有更简单的路线。
  • 因为我正在使用 http get 请求检索另一个页面源..我将整个页面源保存为一个字符串。使用该字符串我找到了那个特定的值。
  • 我还是最喜欢这个答案。即使您通过 http get 请求获取页面,您也可以解析结果字符串中的 html 并使用 jQuery(甚至只是常规的 dom 操作)。见stackoverflow.com/questions/888875/…
  • 是否需要保存页面?或者您可以简单地动态解析 HTML 吗?我问的原因是您可以再次使用 jQuery 和 .load() 等方法来定位您正在发出请求的页面的特定区域并执行操作(在这种情况下加载到您自己页面上的 DOM 节点) 基于结果
【解决方案2】:
// Match against your HTML, store matches in new 'matches' Array
var matches = htmlStr.replace(/<div.*?>(.+?)<\/div>/g, "$1 ").trim().split(' ');

...这假设您尝试检索的值中没有空格,如果空格有效,您需要稍微调整一下。但是针对您提供的测试 HTML:

<div class="fs" id="a_secret">71drwec4</div>
<div class="fs" id="a_secre">51drdsf3</div>
<div class="fs" id="a_secr">54451drwec3</div>

...这种方法产生:

["71drwec4", "51drdsf3", "54451drwec3"]

干杯

【讨论】:

  • 但我只需要检索 id="a_secret 的内容,即 71drwec4
【解决方案3】:

这个怎么样:

var pattern = /<div class="fs" id=".*?">(.*?)<\/div>/gm;
var src = '<div class="fs" id="a_secret">71drwec4</div> \n <div class="fs" id="a_secre">51drdsf3</div> \n <div class="fs" id="a_secr">54451drwec3</div>';
var match;
while (match = pattern.exec(src)) {
  alert(match[1]);
}

根据需要更改它(我不知道您的 id 会是什么样子,或者它们是否都具有相同的类等)。如果您正在寻找匹配您自己页面内的元素,jQuery 会更容易,正如其他海报所提到的那样。

并且...必须引用其他 SO 帖子:RegEx match open tags except XHTML self-contained tags

【讨论】:

    【解决方案4】:

    其中一个可能适用于 javascript。

    保护溢出

    str = '
    <div
      (?=\s) 
      (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) id \s*=
          (?: (?>              \s* ([\'"]) \s* a_secret \s* \g{-1} )
            | (?> (?!\s*[\'"]) \s*             a_secret (?=\s|>) )   
          )
      )
      (?> \s+ (?:".*?"|\'.*?\'|[^>]*?)+ 
    >
      ) (?<! /> )
    
    (?<not_71drwec3>(?:(?!71drwec3).)*?) </div\s*>
    ';
    

    不受的保护

    str = '
    <div
      (?=\s) 
      (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) id \s*=
          (?:               \s* ([\'"]) \s* a_secret \s* \g{-1}
            |  (?!\s*[\'"]) \s*             a_secret (?=\s|>) 
          )
      )
      \s+ (?:".*?"|\'.*?\'|[^>]*?)+ 
    >
      (?<! /> )      // line is worthless when unprotected
    
    (?<not_71drwec3>(?:(?!71drwec3).)*?) </div\s*>
    ';
    

    unprotected no without \g{} notation

    str = '
    <div
      (?=\s) 
      (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) id \s*=
          (?:               \s* ([\'"]) \s* a_secret \s* \1    // Group 1
            |  (?!\s*[\'"]) \s*             a_secret (?=\s|>) 
          )
      )
      \s+ (?:".*?"|\'.*?\'|[^>]*?)+ 
    >
      (?<! /> )      // line is worthless when unprotected
    
    ((?:(?!71drwec3).)*?) </div\s*>                           // Group 2
    ';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      相关资源
      最近更新 更多