【问题标题】:How to retrieve email addresses and names from a string and insert them into a HTML table using Regex?如何从字符串中检索电子邮件地址和姓名并使用正则表达式将它们插入 HTML 表中?
【发布时间】:2019-03-02 02:53:06
【问题描述】:

我有一个字符串,其中包含人员的姓名(名字和姓氏)、用户名和电子邮件地址。我需要获取姓名和电子邮件地址,并使用正则表达式(正则表达式)将它们添加到 HTML 表中。

HTML 表格应如下所示:

到目前为止,这是我的 javascript 代码:

// this is some example of the names & email adresses - they are fake
const outlook = "Anders Jensen (EAAAANJE) <eaaaanje@students.eaax.dk>; Bodil Pedersen (EAAABOPE) <eaaabope@students.eaax.dk>; Åse Andersen (EAAAIDAN) <eaaaasan@students.eaax.dk>; Mühl Svendsen (EAAAPESV) <eaaamusv@students.eaax.dk>";

// we find all the emails & names of the students
let regexEmail = /\<.*?\>/g;
let regexName = /\w+\s\w+\s/gi;

// an array of all the td-tags
let tdTags = document.querySelectorAll("td");

// The emails and names are inserted in the table
for(let i = 0; regexName.exec(outlook) !== null; i++) {

    tdTags[i].innerHTML = regexName.exec(outlook)[i]; // name
    tdTags[i].nextSibling.innerHTML = regexEmail.exec(outlook)[i]; // e-mail
}

问题是它只打印一个名称并将其插入第一个 td。并且无法检索电子邮件地址。 我仍然是 Regex 的初学者,所以我看不出我做错了什么。

任何帮助将不胜感激!

【问题讨论】:

  • Obligatory "don't use regex on HTML"。如果您实际上将姓名和电子邮件放在不同的单元格中,为什么不从每个单元格中获取它们的文本内容?
  • 我需要从字符串中检索姓名和电子邮件地址,然后将它们插入到表中
  • 如果您的格式总是firstName lastName (username) &lt;emailAddress&gt;; 相同,那么您应该能够查找括号和括号以查找用户名和电子邮件。
  • @VLAZ:OP 没有使用正则表达式解析 HTML。你能解释一下这样做有什么问题吗?他只是在构建一个简单的 HTML 结构,并不是说这里有一些嵌套标签的复杂性。我认为当你听到带有正则表达式的 HTML 时,你不应该过度沮丧。

标签: javascript regex


【解决方案1】:

首先使用全局match()。这将只返回捕获的字符串。然后遍历该匹配数组并在每个用户上使用exec() 来分隔名称和电子邮件捕获组。

const regex = /([^ ]+ [^ ]+) \(.*?\) <(.*?)>/g;

const outlook = "Anders Jensen (EAAAANJE) <eaaaanje@students.eaax.dk>; Bodil Pedersen (EAAABOPE) <eaaabope@students.eaax.dk>; Åse Andersen (EAAAIDAN) <eaaaasan@students.eaax.dk>; Mühl Svendsen (EAAAPESV) <eaaamusv@students.eaax.dk>";

const tbody = document.getElementById('users')
  .getElementsByTagName('tbody')[0];
const users = outlook.match(regex);

users.forEach((user, index) => {
  const userRegex = /([^ ]+ [^ ]+) \(.*?\) <(.*?)>/;
  const userInfo = userRegex.exec(user);
  const row = tbody.insertRow(index);
  
  if (userInfo) {
    const nameCell = row.insertCell(0);
    const emailCell = row.insertCell(1);
    nameCell.innerHTML = userInfo[1];
    emailCell.innerHTML = userInfo[2];
  }
  
});
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}

th, td {
padding: 10px;
}
<table id="users">
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
  
  </tbody>
</table>

【讨论】:

    【解决方案2】:

    regexName.exec(outlook).length 是 1,所以你的循环只会运行一次。如果您想这样做,您可能需要从outlook 中删除匹配的字符串。但是,我建议您先将 outlook 字符串除以 ;,然后使用该数组的长度作为循环条件。我相信你的正则表达式很好

    // this is some example of the names & email adresses - they are fake
    const outlook = "Anders Jensen (EAAAANJE) <eaaaanje@students.eaax.dk>; Bodil Pedersen (EAAABOPE) <eaaabope@students.eaax.dk>; Åse Andersen (EAAAIDAN) <eaaaasan@students.eaax.dk>; Mühl Svendsen (EAAAPESV) <eaaamusv@students.eaax.dk>";
    
    // we find all the emails & names of the students
    let regexEmail = /\<.*?\>/g;
    let regexName = /(\w+\s\w+\s)/gi;
    
    // an array of all the td-tags
    let tdTags = [];
    
    let fragments = outlook.split(';');
    // The emails and names are inserted in the table
    for(let i = 0; i < fragments.length; i++) {
      console.log(fragments[i])
      let name = fragments[i].match(regexName)
      let email = fragments[i].match(regexEmail)
    
      tdTags.push({ name, email: email });// e-mail
    }
    

    【讨论】:

    • 我尝试运行循环 10 次,运行条件​​为:i
    • 问题是你每次都得到相同的结果。就像我提到的那样,您需要在每次迭代中删除匹配的字符串,或者用分号拆分字符串并匹配数组中的元素。无论哪种方式都应该工作
    【解决方案3】:

    一旦你有一个字符串,我会使用类似的东西: string.match(/\/g)[0]

    【讨论】:

      【解决方案4】:

      我知道你的问题是关于正则表达式的,但我只会使用旧的 split

      // this is some example of the names & email adresses - they are fake
      const outlook = "Anders Jensen (EAAAANJE) <eaaaanje@students.eaax.dk>; Bodil Pedersen (EAAABOPE) <eaaabope@students.eaax.dk>; Åse Andersen (EAAAIDAN) <eaaaasan@students.eaax.dk>; Mühl Svendsen (EAAAPESV) <eaaamusv@students.eaax.dk>";
      
      // Get an array of the addresses by splitting on ;
      const addresses = outlook.split(';').map(s => s.trim());
      for (let address of addresses) {
        // Split each address on space
        const parts = address.split(' ');
        // the first two should be first and last name
        const name = `${parts[0]} ${parts[1]}`;
        // the fourth element should be the email address
        // remove the angle brackets
        const email = parts[3].replace(/(<|>)/g, '');
        // Do your table thing with the results
        console.log(name, email);
      }

      【讨论】:

        猜你喜欢
        • 2011-12-15
        • 2013-04-09
        • 1970-01-01
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-23
        相关资源
        最近更新 更多