[英]String.split function is not working properly with some text
I have string parser in node.js.我在 node.js 中有字符串解析器。 Input string comes from telegram channel.
输入字符串来自电报频道。 Now I have serious problem with
String.split
function.现在我对
String.split
函数有严重的问题。 It works with some types of text but it doesn't work with some other texts.它适用于某些类型的文本,但不适用于其他一些文本。 When I receive not processed string in telegram, I just copy and send it in the channel again.
当我在电报中收到未处理的字符串时,我只需将其复制并再次发送到频道中。 In this case, parser processes it well.
在这种情况下,解析器处理得很好。 Is there any advise for this issue?
对这个问题有什么建议吗?
let teams = [];
teamSeps =[" vs ", " v ", " - ", " x " ,"-", " -"];
for(let i = 0; i< teamSeps.length; i++){
teams = newTip.Match.toLowerCase().split(teamSeps[i]);
if(teams.length === 2) break;
}
newTip.Home = teams[0].trim();
newTip.Away = teams[1].trim();
return;
Instead of adding multiple options with optional spaces on either side of -
, you can use a single regex with some alternation .相反,在两侧添加可选空间的多个选项
-
你可以使用一个正则表达式的一些交替。
/\s*-\s*|\s+(?:vs|v|x)\s+/
\\s*-\\s*
: Allows optional space around -
\\s*-\\s*
:允许可选空间-
\\s+(?:vs|v|x)\\s+
: Allows at least one space around vs
or v
or x
(Otherwise, if there is a x
or v
in the string, it will split) \\s+(?:vs|v|x)\\s+
: 允许在vs
或v
或x
周围至少有一个空格(否则,如果字符串中有x
或v
,它将被拆分) function customSplit(str) { return str.split(/\\s*-\\s*|\\s+(?:vs|v|x)\\s+/); } console.log(customSplit("Man United vs Man City")) console.log(customSplit("France - Croatia")) console.log(customSplit("Belgium-England")) console.log(customSplit("Liverpool x Spurs"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.