如何在Lambda Node JS中仅从电子邮件中提取名称

时间:2019-04-08 04:40:16

标签: amazon-web-services aws-lambda amazon-dynamodb

嗨,我在node js函数中使用了lambda。我只需要从电子邮件中提取姓名来查询dynamo db。我只需要命名,因为例如:如果我输入 pavi@gamil.com 。我应该收到所有重复的电子邮件,例如

pavi1@gmail.com
pavi2@gmail.com

请帮助我做到这一点  下面是我的代码:

const AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient();


var tableName = "Testing";

exports.handler = (event, context, callback) => {
    console.log(event.PersonName)





docClient.scan(params, function(err, data){
    if(err){
        callback(err, data);
    }else{
        callback(err, data);
   }

在上面的代码中,我直接给出了名称。动态出现时,它应该能够从电子邮件中提取名称,并应该分配给val

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式。

这是从username中获取email的单行代码。

   var emailId = event.emailId; // or any other var which is having emaiID
   var username = emailId.replace(/@.*$/,"");

有关更多用例Regular expression for email

针对您的情况

exports.handler = (event, context, callback) => {
    console.log(event.PersonName)

var emailId = event.emailId; // or any other var which is having emaiID
var val = emailId.replace(/@.*$/,""); 

// use console.log to see if you're getting correct username

var params=  {
    TableName:'Testing',
    KeyConditionExpression : 'EmailID = :EmailID',    
    FilterExpression : 'begins_with (EmailID, :val) ',
    ExpressionAttributeValues : {
       ':val' :  val
   }
};

console output