目标:我想用破折号分割字符串,其中每个数组项都排除空格。
示例:
字符串->“ CL-目的地-机场税”
我已经尝试过:
var splitArray = section.split(/[-]+/);
预期:
splitAray [0] = "CN"
splitAray [1] = "Transit "
splitAray [2] = "Airport Tax"
实际:
splitAray [0] = "CN "
splitAray [1] = " Transit "
splitAray [2] = " Airport Tax"
如何轻松去除任何外部空间?
答案 0 :(得分:2)
将map
与trim
一起使用:
const section = "CL - Destination - Airport Tax";
const splitArray = section.split(/[-]+/).map(s => s.trim());
console.log(splitArray);
ES5语法:
var section = "CL - Destination - Airport Tax";
var splitArray = section.split(/[-]+/).map(function(s) {
return s.trim();
});
console.log(splitArray);
答案 1 :(得分:1)
只需在破折号的两侧添加可选的空格:
var section = "CL - Destination - Airport Tax";
var splitArray = section.split(/\s*[-]\s*/);
console.log(splitArray);
答案 2 :(得分:0)
这里的解决方案图功能将自动修剪数组的每个元素。
CREATE TABLE DB2.BOMHistory AS
SELECT
DB1.ProductInfo.ProductID
DB1.ProductInfo.TimeCreated AS ProductCreated
DB1.StationInfo.StationID
DB1.StationInfo.BOM_used
FROM DB1.ProductInfo
JOIN DB1.StationRecord
ON DB1.ProductInfo.ProductID = DB1.StationRecord.ProductID
JOIN DB1.StationInfo
ON DB1.StationRecord.StationID = DB1.StationInfo.StationID
constraint PK_BOMHistory Primary Key (ProductID,StationID)