映射字符串中的每个单词以重新排序
目前我正在尝试重新排序我的字符串。我相信我需要这样做我使用.map
所以,我需要重新排序字符串Set N Total Games Over/Under N. N代表一个随机数。这存储在market
的变量中。这将需要重新排序N/N总游戏集N.我已经开始通过使用很多凌乱的if语句,并使用substr
来做到这一点,但这不是一个好的解决方案,因为我的代码看起来非常混乱,而不是反正很好的解决方案。我想知道是否有更好的方法来做到这一点。映射字符串中的每个单词以重新排序
至于字符串,每个marketLabel会略有不同,但数字(N)每次可能不同,但如果这有帮助,最大数量N可以是5。
在分码聪明,这是我有:
if (marketLabel.includes('Set' && 'Total Games Over/Under')) {
var splits = 'foo'; // = marketLabel.split('/');
var set = 'foo';
var market = 'foo';
if(marketLabel.includes('Set 1')) {
var arr = marketLabel.split(" ").map(function (val) {
console.log(String(val));
return String(val) + 1;
});
}
if(marketLabel.includes('Set 2')) {
splits = marketLabel.split('Set 2');
set = marketLabel.substr(0, marketLabel.indexOf('2')+1);
return "Under/Over" + splits + " " + set;
}
if(marketLabel.includes('Set 3')) {
splits = marketLabel.split('Set 3');
set = "set 3";
console.log('foo 3');
}
if(marketLabel.includes('Set 4')) {
set = "set 4"
splits = marketLabel.split('Set 4');
console.log('foo 4');
}
if(marketLabel.includes('Set 5')) {
set = "set 5"
splits = marketLabel.split('Set 1');
console.log('foo 5');
}
因此,在总结,我需要的是marketLabel这可能是下列之一:
Set 1 Total Games Over/Under 9.5
Set 2 Total Games Over/Under 9.5
Set 3 Total Games Over/Under 9.5
Set 4 Total Games Over/Under 9.5
Set 5 Total Games Over/Under 9.5
重新-ordered到:
Under/Over 9.5 Total Games Set 1
Under/Over 9.5 Total Games Set 2
Under/Over 9.5 Total Games Set 3
Under/Over 9.5 Total Games Set 4
Under/Over 9.5 Total Games Set 5
Uging正则表达式:
个market = "Set 1 Total Games Over/Under 9.5";
regex = /Set ([0-9.]+) Total Games (Over)\/(Under) ([0-9.]+)/
var match = regex.exec(market);
var newStr = match[3] + '/' + match[2] + ' ' + match[1] + ' Total Games Set ' + match[4];
console.log(newStr);
数字,Over
和Under
字符串捕获并通过打印match
数组元素重新排序。
模式是胜于言:
你也可以捕捉数字和以正确的顺序将它们注入字符串:
market = "Set 1 Total Games Over/Under 9.5";
regex = /Set ([0-9.]+) Total Games Over\/Under ([0-9.]+)/
var match = regex.exec(market);
var newStr = 'Under/Over ' + match[1] + ' Total Games Set ' + match[2];
console.log(newStr);
因此,现在我使用'return ['%/ s%s',%match'[2],match [1]]'正确地显示了浏览器,但实际的'%s'呈现在浏览器。 – DaveDavidson
我编辑添加一个'newStr'变量集的字符串和数组元素的串联。 – SLePort
你应该张贴你的代码 - 我无法真正理解你在问什么。 –