简体   繁体   English

如何将字符串中的所有数字从 1-9 替换为 Javascript 中的拼写等效项?

[英]How can I replace all numbers in a string from 1-9 to their spelled out equivalents in Javascript?

I want to change all numbers ranging from 1-9 to their spelled out equivalents in a string, such as changing all 1 to one, 3 to three, etc.我想将范围从 1-9 的所有数字更改为字符串中的拼写等价物,例如将所有 1 更改为 1,将 3 更改为 3 等。

I tried string.replace() like this:我试过这样的string.replace()

let string = "I have 3 cats";
string = string.replace(/3/g, "three");

This works fine for the above string, but it also changes 33 to threethree which is not what I want.这适用于上述字符串,但它也会将 33 更改为 threethree,这不是我想要的。 I also tried我也试过

string = string.replaceAll(" 3 ", "three);

But this resulted in strings beginning with a number like "5 dollars is a lot of money" being overlooked.但这导致以“5 美元是很多钱”之类的数字开头的字符串被忽略了。 What should I do?我应该怎么办?

Edit: I am sorry if I didn't make my question clear.编辑:如果我没有把我的问题说清楚,我很抱歉。 I wanted numbers higher than 1-9 like 33 to remain as 33, and only change the 9 numbers.我希望高于 1-9 的数字(例如 33)保持为 33,并且只更改 9 个数字。

do you want to replace 3 but not 33 ?你想替换3而不是33吗? you can use regex boundary \b or whole word您可以使用正则表达式边界\b或整个单词

 numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; var s = "I have 3 cats with 33 lives"; result = s.replace(/\b\d\b/g, m => numbers[m]) console.log(result)

 let string = "I have 3 cats. 55 dollars is a lot of money. 2 dollar is fine"; let numbersList = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; string = string.split(" ").map(val => numbersList[val] || val).join(" "); console.log(string);

You could take a function for replacing and search for a digit and replace it with a value.您可以使用 function 来替换和搜索数字并将其替换为值。

The implemented approach works up to 999 .实施的方法可以达到999

 function wordify(n) { n = +n; let word = [], numbers = { 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve', t3: 'thir', t5: 'fif', t8: 'eigh', 20: 'twenty' }, hundreds = 0 | (n % 1000) / 100, tens = 0 | (n % 100) / 10, ones = n % 10; if (n === 0) { return 'zero'; } if (hundreds) { word.push(numbers[hundreds] + ' hundred'); } if (tens === 0) { word.push(numbers[ones]); } else if (tens === 1) { word.push(numbers['1' + ones] || (numbers['t' + ones] || numbers[ones]) + 'teen'); } else { word.push(numbers[tens + '0'] || (numbers['t' + tens] || numbers[tens]) + 'ty'); if (numbers[ones]) { word.push(numbers[ones]); } } return word.join(' '); } let string = "I have 3 cats. 55 dollars is a lot of money." string = string.replace(/\d+/g, wordify); console.log(string);

You need to use a regular expression to identify numbers of variable length & use convert each matched number to its English word equivalent using function parameter.the expression used by you only search for single occurrence of 3. use the following code below:您需要使用正则表达式来识别可变长度的数字并使用 function 参数将每个匹配的数字转换为其等效的英文单词。您使用的表达式仅搜索单次出现的 3。使用以下代码:

    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven',
    8: 'eight',
    9: 'nine',
    10: 'ten',
    11: 'eleven',
    12: 'twelve',
    13: 'thirteen',
    14: 'fourteen',
    15: 'fifteen',
    16: 'sixteen',
    17: 'seventeen',
    18: 'eighteen',
    19: 'nineteen',
    20: 'twenty',
    30: 'thirty',
    40: 'forty',
    50: 'fifty',
    60: 'sixty',
    70: 'seventy',
    80: 'eighty',
    90: 'ninety',
    100: 'hundred',
    1000: 'thousand',
  }
  
  const numberValues = Object.keys(numberText)
    .map((val) => Number(val))
    .sort((a, b) => b - a)
  
  const convertNumberToEnglishText = (n) => {
    if (n === 0) return 'zero'
    if (n < 0) return 'negative ' + convertNumberToEnglishText(-n)
  
    let num = n
    let text = ''
  
    for (const numberValue of numberValues) {
      const count = Math.trunc(num / numberValue)
  
      if (count < 1) continue
  
      if (numberValue >= 100) text += convertNumberToEnglishText(count) + ' '
  
      text += numberText[numberValue] + ' '
      num -= count * numberValue
    }
  
    if (num !== 0) throw Error('Something went wrong!')
  
    return text.trim()
  }

let input ="I have 3 cats and 567 dogs";

// console.log(input.replace(/\d+/g,'T'));
console.log(input.replace(/\d+/g, (match) => { 
    return convertNumberToEnglishText(match)
}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个包含所有唯一 4 位数字的数组,这些数字可以由 JavaScript 中的数字 1-9 组成 - Creating an array of all unique 4 digit numbers that can be made from the numbers 1-9 in JavaScript 如何使用javascript replace从字符串中删除所有数字和所有文本? - How to remove all numbers and all text from string by using javascript replace? 如何使用javascript从字符串中拆分字符(所有语言环境)和数字? - How can I split the characters(all locales) and numbers from a string using javascript? 如何在javascript中将字符串中的所有&#39;\\&#39;和&#39;/&#39;字符替换为&#39; - &#39; - How can i replace all '\' and '/' characters, in a string to '-', in javascript 如何用英语等价物替换所有重音字符 - how to replace all accented characters with English equivalents 如何从JavaScript中的字符串中获取所有数字? - How to grab all numbers from a string in JavaScript? 如何用JavaScript中的空格替换字符串中的所有\\? - How to replace all the \ from a string with space in javascript? JavaScript 如何用 2 个新字符替换字符串中的字符 - JavaScript How can I replace a char from a String with 2 new chars 如何替换字符串的所有匹配项? - How can I replace all matches of a string? 将所有逗号分隔的数字替换为javascript中字符串中的点分隔数字 - replace all comma seperated numbers to dot seperated numbers in string in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM