在字符串中的每个字符串实例后插入空格

我正在尝试用空格分隔字符串中的单词。

例如,我想将"twothousandninehundredfiftyeight"转换为"two thousand nine hundred fifty eight"(以便以后再将其转换为2958,但我已经知道了)

我设法使用数组字典使其工作:

var dictionary = ["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","fourty","forty","fifty","sixty","seventy","eighty","ninety","hundred","thousand"];

具有以下两个功能:

function spaceinsert(x) {
  for (i = 0; i < dictionary.length; i++) {
    if (x.includes(dictionary[i])) {
      var pos = getIndicesOf(dictionary[i],x);
      for (g = 0; g < pos.length; g++) {
            if (pos[g] != -1 && pos[g] !=0) {
                x = [x.slice(0,pos[g])," ",x.slice(pos[g])].join('');    
            }
      }
    }
  }
  return x
}

function getIndicesOf(searchStr,str,caseSensitive) {
    var searchStrLen = searchStr.length;
    if (searchStrLen == 0) {
        return [];
    }
    var startIndex = 0,index,indices = [];
    if (!caseSensitive) {
        str = str.toLowerCase();
        searchStr = searchStr.toLowerCase();
    }
    while ((index = str.indexOf(searchStr,startIndex)) > -1) {
        indices.push(index);
        startIndex = index + searchStrLen;
    }
    return indices;
}

(抱歉,代码混乱,这是我的新手)

此功能适用于大多数数字。例如:

onethousandthreehundredsixtyfive转换为one thousand three hundred sixty five,但无法处理其他数字,例如seventhousandtwohundredtwentytwo 它将转换为seven thousand two hundredtwent ytwosixthousandfourhundredeightyeight转换为six thousand four hundred eight yeight,我认为这与重复出现的“单词”有关( TWO hundredtwenty TWO ),但我不知道找出解决这个问题的方法。

有什么想法吗?

introject 回答:在字符串中的每个字符串实例后插入空格

按字典长度排序,因此,将首先处理最长的单词,例如,防止“十四”分裂为“四十”。

通过用|将字典词连接起来并用括号括起来,从而创建一个RegExp。使用标志gi(全局,不区分大小写)。有关RegExp的更多信息,请参见regex101

现在将String.replace()与彭定康一起使用,并将每个匹配项替换为match +空格。修剪结果以删除字符串后的空格。

const str = "twothousandninehundredfiftyeight"

var dictionary = ["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","fourty","forty","fifty","sixty","seventy","eighty","ninety","hundred","thousand"].sort((a,b) => b.length - a.length)

const pattern = new RegExp(dictionary.join('|'),'gi')

const result = str.replace(pattern,'$& ').trim()

console.log(result)

,

您可以一次性完成两项工作。无需用空格分隔单词,然后将新字符串转换为数字。只需一次将字符串循环一个单词,然后就可以进行转换。

为此,您需要一个dictionary对象数组,如下所示:

var dictionary = [ { name: "one",value: 1 },{ name: "two",value: 2 },... ];

您必须将复合数字放在首位的地方,这意味着fourteensixteenseventeeneighteennineteen,{{1 }}(这是错误的),fourtysixtyseventyeighty应排在第一位,以便它们具有更高的优先级,因此{{1} }将与ninety匹配,而不与sixty匹配,这将保留sixty,这将导致错误。最终的six数组是:

ty

一次完成两项工作的代码是:

dictionary

示例:

const dictionary = [ { name: "fourteen",value: 14 },{ name: "sixteen",value: 16 },{ name: "seventeen",value: 17 },{ name: "eighteen",value: 18 },{ name: "nineteen",value: 19 },{ name: "fourty",value: 40 },{ name: "sixty",value: 60 },{ name: "seventy",value: 70 },{ name: "eighty",value: 80 },{ name: "ninety",value: 90 },{ name: "one",{ name: "three",value: 3 },{ name: "four",value: 4 },{ name: "five",value: 5 },{ name: "six",value: 6 },{ name: "seven",value: 7 },{ name: "eight",value: 8 },{ name: "nine",value: 9 },{ name: "ten",value: 10 },{ name: "eleven",value: 11 },{ name: "twelve",value: 12 },{ name: "thirteen",value: 13 },{ name: "fifteen",value: 15 },{ name: "twenty",value: 20 },{ name: "thirty",value: 30 },{ name: "forty",{ name: "fifty",value: 50 },{ name: "hundred",value: 100 },{ name: "thousand",value: 1000 } ];

本文链接:https://www.f2er.com/3141822.html

大家都在问