[英]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.