0

我在做这件事上遇到了最困难的时期。

我有以下 HTML,我试图从中解析地址“100 E Faith Ter”

    <span style="border-color: rgb(0, 0, 255); border-width: 2px; border-style: solid;"                 class="addr">     100 E Faith Ter<br>            <span class="locality">Maitland</span>,                     <span class="region">FL</span>            <span class="postal-code">32751</span>                    </span>l-code">32751</span>            </span>

我试图将上面的这个 html 传递给这组代码,但我没有得到匹配,我不明白为什么即使我在正则表达式测试器中尝试它并且它有效。我快疯了。

    function demoShowMatchClick(addressToPull) {
      var re = new RegExp("\s{5}(.*?<br>)");
      var m = re.exec(addressToPull);
      if (m == null) {
        alert("No match");
      } else {
        var s = "Match at position " + m.index + ":\n";
        for (i = 0; i < m.length; i++) {
          s = s + m[i] + "\n";
        }
        alert(s);
      }
    }

我在这里想念什么?

4

2 回答 2

0

这对我有用:

var re = new RegExp("\\s{5}(.*?<br>)");

转义 \s

如果您使用文字正则表达式语法,则不必担心字符串转义:

var re = /\s{5}(.*?<br>)/
于 2013-02-02T06:04:37.893 回答
0
you have to make following changes in your function

 function demoShowMatchClick(addressToPull) {
      var re = \s{5}(.*?<br>);
      var m = re.exec(addressToPull);
      if (re.test(addressToPull)) {
        var s = "Match at position " + m.index + ":\n";
        for (i = 0; i < m.length; i++) {
          s = s + m[i] + "\n";
            alert(s);
      } else {

             alert("No match");
        }

      }
    }
于 2013-02-02T06:13:33.130 回答