【问题标题】:javascript extract email text-parts before and afterjavascript在之前和之后提取电子邮件文本部分
【发布时间】:2016-09-25 14:45:40
【问题描述】:

我需要从字符串中提取一封电子邮件。 所需的输出是包含电子邮件之前的文本部分的数组, 电子邮件, 以及邮件后面的文字。

selection = 'Integer lectus nisi, facilisis sit amet@blabla.test eleifend nec, pharetra ut augue.';

part[0] = 'Integer lectus nisi, facilisis sit ';
part[1] = 'amet@blabla.test';
part[2] = ' eleifend nec, pharetra ut augue.';

到目前为止,我有一个提取电子邮件的功能,但我还需要之前和之后的部分。

function extractEmails(selection){
     return selection.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
};

$output = extractEmails(selection); //returns 'amet@blabla.test'

如何做到这一点?谢谢!

【问题讨论】:

  • 我有一个提取电子邮件的功能。但我也需要前后都有零件。函数 extractEmails(selection){ 返回 selection.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9 ._-]+)/gi); };
  • 谢谢!您需要将其添加到您的问题中,以便我们可以看到您在做什么;单击edit 链接(此处的链接或问题标签下方的链接),以便正确格式化代码。展示函数,展示你是如何调用它的,解释你遇到过或正在遇到的问题;解释它不应该做的事情,以及它不应该做的事情。 :)

标签: javascript string email extract


【解决方案1】:

此代码可能对您有所帮助:

<html>
<head>
<script>
    function extractEmails(selection){
        return selection.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
    };

    function parse(){
       var input = document.getElementById("input").innerText;
       var email = extractEmails(input);
       var subject = input.substring(0, input.indexOf(email));
       var content = input.substring(input.indexOf(email) + email.toString().length);

       // Print out parts
       var output = document.getElementById("output");
       output.innerText = `Subject: ${subject}\nEmail: ${email}\nContent: ${content}`;
    }
</script>
</head>

<body onLoad="parse()">
    Input: 
    <div id="input">Integer lectus nisi, facilisis sit amet@blabla.test eleifend nec, pharetra ut augue.</div>

    <hr/>
    Output: 
    <div id="output"></div>
</body>

</html>

【讨论】:

    【解决方案2】:

    你可以试试这个方法,

    var selection = 'Integer lectus nisi, facilisis sit amet@blabla.test eleifend nec, pharetra ut augue.';
    
    var email = selection.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)[0];
    
    //console.log(email);
    
    var desiredElement = selection.split(email);
    
    Array.prototype.insert = function (index, item) {
      this.splice(index, 0, item);
    };
    
    desiredElement.insert(1, email);
    console.log(desiredElement);
    

    js 小提琴:https://jsfiddle.net/6gg93ff6/5/

    【讨论】:

      【解决方案3】:
      <script>
      function is_email(email)
      {
          var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
          return re.test(email);
      }
      
      var selection = 'Integer lectus nisi, facilisis sit amet@blabla.test eleifend nec, pharetra ut augue.';
      var before = new Array();
      var email = "";
      var after = new Array();
      
      var email_found = false;
      var selection_parts = selection.split(" ");
      for (var i = 0 ; i < selection_parts.length ; i++)
      {
        if (is_email(selection_parts[i]))
        {
          email = selection_parts[i]
          email_found = true;
        }
        else
        {
          if(email_found) after.push(selection_parts[i]);
          else before.push(selection_parts[i]);
        }
      }
      final_array = [before.join(" "), email, after.join(" ")]
      console.log(final_array);
      </script>
      

      【讨论】:

        【解决方案4】:

        感谢您的回答和建议!

        我可以解决这个问题:

         function extractEmails(string){
                return string.split(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
            };
        
            function parse(string){
                var i;
                var array = extractEmails(string);
                var myAssociativeArr = [];
        
                for (i = 0; i < array.length; ++i) {
                    var newElement = {};
                    if(array[i].match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)){
                        newElement['email'] = '[email_image,string=' + array[i] + ',id=2]';
                    }else{
                        newElement['text'] = array[i];
                    }
                    myAssociativeArr.push(newElement);
                }
                console.log(myAssociativeArr);
            }
        
            var string = 'Integer lectus nisi, facilisis sit email1@blabla.test eleifend nec, pharetra ut augue Duis aute irure dolor in reprehenderit in voluptate velit esse email2@blabla.test cillum dolore eu fugiat nulla pariatur.';
            parse(string);
        

        输出:

        [Object { text="Integer lectus nisi, facilisis sit "}, 
        Object { email="[email_image,string=email1@blabla.test,id=2]"}, 
        Object { text=" eleifend nec, pharetra ...n voluptate velit esse "}, 
        Object { email="[email_image,string=email2@blabla.test,id=2]"}, 
        Object { text=" cillum dolore eu fugiat nulla pariatur."}]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-14
          • 2013-10-11
          • 1970-01-01
          • 2016-04-24
          • 1970-01-01
          • 2011-02-26
          • 2022-08-02
          • 1970-01-01
          相关资源
          最近更新 更多