2016-04-01 90 views
2

這聽起來很簡單,因爲我沒有經驗,因爲我想處理JavaScript,我想從字符串中刪除電子郵件,實際上是一個錯誤消息,同時嘗試登錄或註冊。如何從JavaScript中刪除電子郵件(GTM)

我的錯誤是這樣的:

以下電子郵件[email protected]是不正確 無效數據 必須關閉當前會話 郵件已經註冊 ...

這樣的想法是從第一個錯誤中刪除電子郵件,如果沒有電子郵件存在,則保留現在的其他錯誤。 ([^。@ \ s] +)(\。[^​​。@ \ s]

我發現此代碼來檢測字符串中是否存在電子郵件,但無法找到使其工作的方式。 +)*([^。@ \ s] + \。)+([^。@ \ s] +)

我只是想將數據發送到谷歌分析,我試圖避免發送個人數據。

在此先感謝。

+0

另一種選擇是將事件發送到由特定的錯誤消息或錯誤事件觸發GA。通過這種方式,您可以避免使用Javascript以正則表達式刪除字符串,並且您可以自己定製數據(事件類別,操作,標籤),並且您肯定可以避免PII問題。 – nyuen

回答

2

"([^[email protected]\s]+)(\.[^[email protected]\s]+)*@([^[email protected]\s]+\.)+([^[email protected]\s]+)"是一個正則表達式。它匹配看起來像電子郵件的所有內容。您可以將它與replace()search() JavaScript函數一起使用,您需要用兩個/來劃分表達式。

例子:

var myString = "Hello, my email is [email protected]"; 

// Check if there is an email 
if(myString.search(/([^[email protected]\s]+)(\.[^[email protected]\s]+)*@([^[email protected]\s]+\.)+([^[email protected]\s]+)/) !== -1){ 
    console.log("There is an email !"); 
    // Remove it... 
    myString = myString.replace(/([^[email protected]\s]+)(\.[^[email protected]\s]+)*@([^[email protected]\s]+\.)+([^[email protected]\s]+)/,""); 
    console.log(myString); // Hello, my email is 
} 

關於JavaScript的正則表達式很好的教程:http://www.w3schools.com/js/js_regexp.asp

+0

謝謝你,字符串工作得很好,甚至包括連字符和下劃線。我即將發佈它,看看它是如何發展的。 –

相關問題