拆分 Javascript 字符串
Split a Javascript string
可用时间:
周一
周二
周三
8-12
12-5
其他类型的联系人:
短信
不可用时间:
周四
8-12
我想将上面的字符串拆分为三个单独的数组
第一个数组 = [星期一
周二
周三
8-12
12-5]
第 2 个数组 = [SMS]
第 3 个数组 = [星期四
8-12
]
我试过使用拆分功能,但是没用!
感谢您的帮助。
提前致谢
只要字符串的粗体部分始终相同,您就可以尝试这样的操作:
var s = "Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12"
var arr = []
arr.push(s.split(" Unavailable Times: ")[1])
s = s.split(" Unavailable Times: ")[0]
arr.unshift(s.split(" Other type of contact: ")[1])
s = s.split(" Other type of contact: ")[0]
arr.unshift(s.split("Available Times: ")[1])
console.log(arr)
使用拆分 regex
let str = 'Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12'
let replace = str.replace('Available Times: ', '');
let arr = replace.split(/ Other type of contact: | Unavailable Times: /)
console.log(arr);
Does the OP mean ["Monday Tuesday Wednesday 8-12 12-5"]
or ["Monday", "Tuesday", "Wednesday", "8-12", "12-5"]
and likewise for the two remaining ones ?.. or ... does the OP even mean a single array with three string items like ... ["Monday Tuesday Wednesday 8-12 12-5", "SMS", "Thursday 8-12"]
. The question arises since the OP did not provide a proper syntax of the expected result and three separate arrays each with just a single item does not make any sense.
如果只是将信息提取为三个分隔的字符串值,则以下正则表达式可能会有所帮助...
用简单的String.prototype.match
one would receive an array which holds the match itself followed by the results of the 3 capturing groups
为了只接收一个没有 匹配的数组 需要 slice
数组。
也可以利用named capturing groups via RegExp.prototype.exec
and some Destructuring Assignment
const sampleData =
'Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12';
// see ... [https://regex101.com/r/jVedS4/1]
const regXGroups =
/Available Times:\s*(?<available>.*?)\s+Other type of contact:\s*(?<contact>.*?)\s+Unavailable Times:\s*(?<unavailable>.*?)\s*$/;
console.log(
sampleData.match(regXGroups)
);
console.log(
sampleData.match(regXGroups).slice(1)
);
const {
unavailable,
available,
contact,
} = regXGroups.exec(sampleData)?.groups ?? {};
console.log([available, contact, unavailable]);
.as-console-wrapper { min-height: 100%!important; top: 0; }
可用时间: 周一 周二 周三 8-12 12-5 其他类型的联系人: 短信 不可用时间: 周四 8-12
我想将上面的字符串拆分为三个单独的数组 第一个数组 = [星期一 周二 周三 8-12 12-5]
第 2 个数组 = [SMS]
第 3 个数组 = [星期四 8-12 ]
我试过使用拆分功能,但是没用! 感谢您的帮助。 提前致谢
只要字符串的粗体部分始终相同,您就可以尝试这样的操作:
var s = "Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12"
var arr = []
arr.push(s.split(" Unavailable Times: ")[1])
s = s.split(" Unavailable Times: ")[0]
arr.unshift(s.split(" Other type of contact: ")[1])
s = s.split(" Other type of contact: ")[0]
arr.unshift(s.split("Available Times: ")[1])
console.log(arr)
使用拆分 regex
let str = 'Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12'
let replace = str.replace('Available Times: ', '');
let arr = replace.split(/ Other type of contact: | Unavailable Times: /)
console.log(arr);
Does the OP mean
["Monday Tuesday Wednesday 8-12 12-5"]
or["Monday", "Tuesday", "Wednesday", "8-12", "12-5"]
and likewise for the two remaining ones ?.. or ... does the OP even mean a single array with three string items like ...["Monday Tuesday Wednesday 8-12 12-5", "SMS", "Thursday 8-12"]
. The question arises since the OP did not provide a proper syntax of the expected result and three separate arrays each with just a single item does not make any sense.
如果只是将信息提取为三个分隔的字符串值,则以下正则表达式可能会有所帮助...
用简单的
String.prototype.match
one would receive an array which holds the match itself followed by the results of the 3 capturing groups为了只接收一个没有 匹配的数组 需要
slice
数组。也可以利用named capturing groups via
RegExp.prototype.exec
and some Destructuring Assignment
const sampleData =
'Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12';
// see ... [https://regex101.com/r/jVedS4/1]
const regXGroups =
/Available Times:\s*(?<available>.*?)\s+Other type of contact:\s*(?<contact>.*?)\s+Unavailable Times:\s*(?<unavailable>.*?)\s*$/;
console.log(
sampleData.match(regXGroups)
);
console.log(
sampleData.match(regXGroups).slice(1)
);
const {
unavailable,
available,
contact,
} = regXGroups.exec(sampleData)?.groups ?? {};
console.log([available, contact, unavailable]);
.as-console-wrapper { min-height: 100%!important; top: 0; }