仅显示变量内的一段文本

Show only a piece of a text that is inside a variable

我有这个代码:

data = {
   date: '01/01/01 - My Name'
}


<% for(var i = 0; i < data.length; i++) { %>
    <td> <%= data[i].date %>   </td> // I want show just the date here
    <td> <%= data[i].date %>   </td> // I want show just the name here
<% } %>

如何才能在第一个 td 中只显示日期,在第二个中只显示姓名?

是的,我现在可以将两者分开,但我不能那样做..这只是我需要的示例,在适当的情况下我不能那样做。

可以简单地使用split(' - ')从字符串

创建数组
<% for(var i = 0; i < data.length; i++) {
  var dateParts = data[i].date.split(' - '); // returns ['01/01/01', 'My Name']
 %>
    <td> <%= dateParts[0] %>   </td> // I want show just the date here
    <td> <%= dateParts[1] %>   </td> // I want show just the name here
<% } %>

假设在-前后有一个space,如示例

所示