如何在玉中复杂的条件或正则表达式

时间:2013-03-30 00:27:00

标签: javascript node.js express pug

我有一个有趣的挑战,无法弄清楚如何解决它。我从我的mailchimp中提取订阅者列表并填充成员'我的网站上的数据页面。我注册的一个领域是,"网站或LinkedIn个人资料网址。"

我想做的是,检查他们是否提供了一个地址,如果是这样,请将他们的网址放入linkedin会员插件中,如果不使用其他数据来创建更简单的个人资料,名称网站,标题等。

我的问题是,我无法说明jade中的data.websiteField.match(/ linkedin /),所以我要么将数据传递给我遇到问题的客户端javascript。做点什么。

这是从mailchimp

返回的数据样本
[ // this first bit is just the fields
  [
    "email address",
    "first name",
    "last name",
    "role",
    "organization",
    "headline",
    "website",
    "areas of interest",
    "please email me about:",
    "member_rating",
    "optin_time",
    "optin_ip",
    "confirm_time",
    "confirm_ip",
    "latitude",
    "longitude",
    "gmtoff",
    "dstoff",
    "timezone",
    "cc",
    "region",
    "last_changed"
  ],
  [ // and here's the first user
    "woop@booomshakala.com", // haha, just hiding some data
    "Woop",
    "Shak",
    "Tester",
    "ShakaCo",
    "Whooooooper",
    "http://linkedin.com/in/costamichailidis", // hit me up sometime
    "Creativity, Innovation, Ideas and Science",
    "Job Placement, Internships, Recruitment & Retention, Technology & MOOCs, Measurement & Evaluation, Documentation & Dissemination",
    2,
    "2013-03-28 19:28:55",
    "173.52.122.111",
    "2013-03-28 19:29:12",
    "173.52.122.111",
    "40.7648000",
    "-73.7775000",
    "-5",
    "-4",
    "America/New_York", "US", "NY", "2013-03-28 19:29:12"
  ]
]

任何帮助都会摇滚!

另外,我使用快递。表示在客户端javascript中使本地可用吗?

1 个答案:

答案 0 :(得分:2)

客户端正则表达式

Jade允许您使用行尾的-修饰符执行任意javascript

- if (profile.websiteField.match(/linkedin/)
    // render linkedin profile here
- else
    // render simple profile here

服务器端正则表达式

我认为格式化配置文件信息服务器端并将renderLinkedIn布尔字段设置为true或false

会更简单
function displaySignUpPage(req, res) {
  var profile = formatMailChimpData()
  // profile now looks like
  // {
  //   "email address": "woop@booomshakala.com",
  //   "first name": "Noah",
  //   ...
  // }

  var linkedInRegex = /linkedin/;
  profile.renderLinkedIn = linkedInRegex.test(profile.website) // set renderLinkedIn to true or false
  // say your jade view is called signUpPage.jade
  var pageData = {
    title: 'Register',
    profile: profile
  }
  res.render('signUpPage', pageData)
}


function formatMailChimpData() {
  var mailChimpData = [
    [
      "email address",
      "first name",
      "last name",
      "role",
      "organization",
      "headline",
      "website"
      // other fields truncated
    ],
    [
      "woop@booomshakala.com", // haha, just hiding some data
      "Noah",
      "Isaacson",
      "Tester",
      "ShakaCo",
      "Whooooooper",
      "http://www.linkedin.com/pub/noah-isaacson/59/6a2/553"
    ]
  ]


  // mailChimp puts the keys as the first entry
  var mailChimpFields = mailChimpData[0]
  var mailChimpProfile = mailChimpData[1]
  // make profile into key-value pairs
  var keyValuePairs = mailChimpFields.map(function (field, index) {
    var profileValue = mailChimpProfile[index]
    var keyValuePair = [field, profileValue]
    return keyValuePair
   })
  var profile = keyValuePairs.reduce(function(prev, pair) {
    var key = pair[0]
    var value = pair[1]
    prev[key] = value
    return prev
  }, {})
  return profile
}