在JavaScript数组字符串中获取相邻字符的最佳方法是什么?

时间:2016-01-30 19:41:17

标签: javascript arrays string

我们说我有这个:

var arr = [ "Exploring the zoo, we saw every kangaroo jump and quite a few carried babies.", "The wizard quickly jinxed the gnomes before they vaporized.", "The quick brown fox jumps over the lazy dog."];

我试图找出一种方式,当我输入" jinxed"时,我也快速地得到了#34;" "之前的侏儒"。没有必要整个单词,我只想在输入之前和之前得到几个字符。我一直在围墙上撞墙,所以我很感激任何建议和提示。

谢谢!

3 个答案:

答案 0 :(得分:1)

使用split获取之前的内容以及jinxed之后的内容,然后使用substr获取每个部分中的最后一个或第一个字符:

var a = "The wizard quickly jinxed the gnomes before they vaporized";
var s = a.split('jinxed');

s == [ 'The wizard quickly ',' the gnomes before they vaporized' ]

s[0].substr(-10,10) == 'd quickly '
s[1].substr(0,10) == ' the gnome'

答案 1 :(得分:1)

您可以使用RegEx匹配:

function search(s,q, msg) {
  // match the characters before and after
  var m = s.match(new RegExp("(.|^)" + q + "(.|$)"));
console.log(m);
  // if there was a match at all, then drop the 1st (which is the whole match: " jinxed ")
  if (m) {
    if(m.length>1) {m = m.splice(1)}
    // m[0] in the character before, m[1] is the one after
    alert(msg + " '" + m[0] + "', '" + m[1] + "'");
  } else {
    alert(msg + " not found");
  }
}

search("The wizard quickly jinxed the gnomes before they vaporized.","jinxed", "in the middle");
search("jinxed the gnomes before they vaporized.","jinxed", "in the beginning");
search("The wizard quickly jinxed","jinxed","in the end");
search("jinxed","jinxed","full");
search("The wizard quickly","jinxed","not in string");

请注意,当您搜索的字符串位于该行的开头或结尾时,这也适用。

答案 2 :(得分:1)

DECLARE @startDateTime DATETIME = '2016-01-01 00:00:00'
DECLARE @endDateTime DATETIME = '2016-01-31 23:59:59'

;WITH sales AS
(
  SELECT 
    ev.ID,
    ISNULL(SUM(jiv.Amount), 0) AS TotalSales,
    MONTH(j.Created) AS [Month],
    YEAR(j.Created) AS [Year]
  FROM Employees_View AS ev
  LEFT JOIN Job_Items_View AS jiv ON jiv.Emp_ID = ev.ID
  LEFT JOIN Jobs AS j ON j.ID = jiv.Job_ID
  WHERE j.Created BETWEEN @startDateTime AND @endDateTime
  GROUP BY 
    ev.ID,
    MONTH(j.Created),
    YEAR(j.Created)
),
commissions AS
(
  SELECT
    s.ID,
    CASE ev.PayUnitCode 
      WHEN 'C' THEN s.TotalSales * (ev.PayRate / 100)   
      WHEN 'SC' THEN (SELECT SUM(Amount) FROM Job_Items_View) * (ev.Commission / 100)
      ELSE 0
    END AS TotalCommission
  FROM sales AS s
  JOIN Employees_View AS ev ON ev.ID = s.ID
),
salaries AS
(
  SELECT 
    ID, 
    CASE PayUnitCode 
      WHEN 'C' THEN 0 
      ELSE PayRate 
    END AS Salary 
  FROM Employees_View 
),
totals AS
(
  SELECT 
    salaries.ID,
    ISNULL(sales.Month, MONTH(@startDateTime)) AS [Month],
    ISNULL(sales.Year, YEAR(@startDateTime)) AS [Year],
    ISNULL(sales.TotalSales, 0) AS TotalSales,
    salaries.Salary,
    ISNULL(commissions.TotalCommission, 0) AS TotalCommission
  FROM salaries
  LEFT JOIN sales ON salaries.ID = sales.ID
  LEFT JOIN commissions ON commissions.ID = sales.ID
)
SELECT 
  ev.PayRate,
  ev.Name,
  t.Salary + t.TotalCommission AS Pay,
  LEFT(DATENAME(MONTH, DATEADD(MONTH , t.[Month], -1)), 3) 
    + '-' + CAST(t.[Year] AS VARCHAR) AS [Month],
  ev.ID AS Emp_ID,
  pu.Name AS PayUnit,
  ev.Commission
FROM totals AS t
JOIN Employees_View AS ev ON ev.ID = t.ID
JOIN PayUnits AS pu ON pu.Code = ev.PayUnitCode