2016-09-25 13 views
0

文字列から電子メールを抽出する必要があります。 希望する出力は、電子メールの前にテキスト部分、 の電子メール、 、および電子メールの後のテキストを含む配列です。javascriptはメールの前後にテキスト部分を抽出します

selection = 'Integer lectus nisi, facilisis sit [email protected] eleifend nec, pharetra ut augue.'; 

part[0] = 'Integer lectus nisi, facilisis sit '; 
part[1] = '[email protected]'; 
part[2] = ' eleifend nec, pharetra ut augue.'; 

これまでのところ、電子メールを抽出する機能はありますが、前後の部品も必要です。

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

$output = extractEmails(selection); //returns '[email protected]' 

どうすればいいですか?ありがとう!あなたがこの方法を試すことができます

+0

私は電子メールを抽出する機能を有しています。しかし、私はまた、前と後の部分を持つ必要があります。 (a-zA-Z0-9。_-] + @ [a-zA-Z0-9 ._-] + \。[a-zA-Z0 -9) - )+)/ gi); }; –

+0

ありがとう!それをあなたの質問に追加する必要があります。そうすれば、あなたがしていることを見ることができます。コードを適切にフォーマットできるように[編集]リンク(ここにあるものか質問のタグの下にあるもの)をクリックしてください。機能の表示、呼び出し方法の表示、問題の説明、またはそれに伴う問題の説明。それがしてはならないことと、それがしなければならないことを説明してください。 :) –

答えて

0

このコードはあなたを助けるかもしれない:あなたの答えや提案のための

<html> 
<head> 
<script> 
    function extractEmails(selection){ 
     return selection.match(/([a-zA-Z0-9._-][email protected][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 [email protected] eleifend nec, pharetra ut augue.</div> 

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

</html> 
0

var selection = 'Integer lectus nisi, facilisis sit [email protected] eleifend nec, pharetra ut augue.'; 

var email = selection.match(/([a-zA-Z0-9._-][email protected][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/

0
<script> 
function is_email(email) 
{ 
    var re = /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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 [email protected] 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> 
0

感謝を!

私はそれを仕事ができる:

function extractEmails(string){ 
     return string.split(/([a-zA-Z0-9._-][email protected][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._-][email protected][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 [email protected] eleifend nec, pharetra ut augue Duis aute irure dolor in reprehenderit in voluptate velit esse [email protected] cillum dolore eu fugiat nulla pariatur.'; 
    parse(string); 

出力:

[Object { text="Integer lectus nisi, facilisis sit "}, 
Object { email="[email_image,[email protected],id=2]"}, 
Object { text=" eleifend nec, pharetra ...n voluptate velit esse "}, 
Object { email="[email_image,[email protected],id=2]"}, 
Object { text=" cillum dolore eu fugiat nulla pariatur."}] 
関連する問題