简体   繁体   English

按所有非数字字符拆分字符串

[英]Split string by all non-numeric characters

I have a string like this:我有一个这样的字符串:

let s = '10p6s23'

I'd like to split it in such a way that I get the numbers and the letters in an array, so ideally I'd get this:我想以这样一种方式拆分它,即我得到数组中的数字和字母,所以理想情况下我会得到这个:

 [10,'p',6,'s',23]

The ultimate goal is to build a add/subtract calculator where p = + and s = -.最终目标是构建一个加法/减法计算器,其中 p = + 和 s = -。 So the string above would resolve to 10 + 6 - 23 = -7 .所以上面的字符串将解析为10 + 6 - 23 = -7

What I've tried我试过的

Split string into array on first non-numeric character in javascript 在 javascript 中的第一个非数字字符处将字符串拆分为数组

This worked well, except it only split on the first char, not all chars.这很有效,除了它只在第一个字符上拆分,而不是在所有字符上拆分。

 const regexp = /(\d+|[az]+)/g; const str = '10p6s23'; const array = [...str.match(regexp)]; console.log(...array); let replaced = array.map(item => { switch(item) { case 'p': return '+'; case 's': return '-'; default: return item; } }).join(''); console.log(eval(replaced));

One way to do it is by using a Regular Expression一种方法是使用正则表达式

 let s = '10p6s23' const numbers = [...s.matchAll(/\d+/g)].map((num) => parseInt(num)); const operators = [...s.matchAll(/\D+/g)]; console.log(numbers); console.log(operators);

And work from here with the rest of the logic并从这里开始使用逻辑的 rest

you can use match function here.你可以在这里使用匹配 function。

'10p6s23'.match(/[a-zA-Z]+|[0-9]+/g)

// /[a-zA-Z]+|[0-9]+/g this is the regular expression for numbers or letters

You could split on:你可以拆分:

  • digits preceded by a letter ie (?<=[az])\d+ or以字母开头的数字,即(?<=[az])\d+
  • letters preceded by a digit ie (?<=\d)[az]+以数字开头的字母,即(?<=\d)[az]+

Afterwards, you will have the filter on non-empty values.之后,您将对非空值进行筛选。

 const regexp = /((?<=[az])\d+|(?<=\d)[az]+)/ig; const str = '10p6s23'; const array = str.split(regexp).filter(x => x.length); console.log(...array);

Here is an iterative replacement solution:这是一个迭代替换解决方案:

 const compute = (input) => { let count = input.match(/[ps]/g).length, result = input; while (count --> 0) { result = result.replace( /(\d+)([ps])(\d+)/, (_, left, op, right) => { switch (op) { case 'p': return parseInt(left, 10) + parseInt(right, 10); case 's': return parseInt(left, 10) - parseInt(right, 10); } } ); } return result; }; console.log(compute('10p6s23'));

暂无
暂无

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

相关问题 从 JavaScript 中的字符串中去除所有非数字字符 - Strip all non-numeric characters from string in JavaScript 来自Javascript中包含非数字字符的字符串的数字数组 - Numeric array from string containing non-numeric characters in Javascript 将字符串拆分为javascript中第一个非数字字符的数组 - Split string into array on first non-numeric character in javascript 删除除一个特定字符以外的所有非数字字符 - Remove all non-numeric characters except one particular one 如何从变量中删除所有非数字字符 - How to remove all non-numeric characters from a variable 从javascript中的字符串中剥离逗号和非数字字符 - Stripping commas and non-numeric characters from string in javascript 如何使用名为 val 的字符串类型的参数创建一个函数并返回一个提取/消除所有非数字字符的数字; - How do I make a function with a parameter called val of type string and return a number extracting/eliminating all non-numeric characters; 我需要输入一个字符串,取出非数字字符,然后如果字符串以1开头,则删除1 - I need to take an inputted string, take the non-numeric characters out, and then if the string begins with a 1, remove the 1 如何在排除句点时替换字符串中的非数字字符? - How do I replace non-numeric characters in string while excluding periods? 当字符串包含非数字字符时,parseFloat()如何工作? - How does parseFloat() work when the string contains non-numeric characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM