[英]Split string into array on first non-numeric character in javascript
I want to split a string into an array containing all leading digits as one value and the rest of the string as the second value:我想将一个字符串拆分为一个数组,其中包含所有前导数字作为一个值,并将字符串的其余部分作为第二个值:
3px -> Array('3','px')
01b -> Array('01','b')
01 -> Array('01','')
b -> Array('','b')
1.5 -> Array('1','.5')
300,5 -> Array('300',',5')
a1 -> Array('','a1')
In PHP you could do something like:在 PHP 中,您可以执行以下操作:
$matches = array();
preg_match('/([0-9]+)([^0-9]+)/',$value,$matches);
I'd use .match
instead of split
: match and capture digits, then match and capture anything else in a different capture group:我会使用.match
而不是split
:匹配并捕获数字,然后匹配并捕获不同捕获组中的任何其他内容:
const doSplit = str => { const [, ...arr] = str.match(/(\\d*)([\\s\\S]*)/); return arr; }; console.log( doSplit('3px'), doSplit('01b'), doSplit('01'), doSplit('b'), );
(\\d*)
- matches digits (\\d*)
- 匹配数字([\\s\\S]*)
- matches absolutely anything after that ([\\s\\S]*)
- 完全匹配之后的任何内容只需执行str.match(/([0-9]+)([^0-9]+)/).splice(1)
其中str
是包含您的值的变量。
You can do somethinglike this:你可以这样做:
var str = "Hello world!";
var res = [str.substring(0, 1), str.substring(1)];
This stores the string in a variable, and then creates an array containing 2 elements.这将字符串存储在一个变量中,然后创建一个包含 2 个元素的数组。 The first will be a substring of the string containing the value from the start to the 1st position, and the second value will be a substring containing the value from the second position, and as no second parameter is supplied, until the end.第一个将是包含从开始到第一个位置的值的字符串的子字符串,第二个值将是包含从第二个位置开始的值的子字符串,并且没有提供第二个参数,直到结束。
Here is my take on this one.这是我对此的看法。 Hope it helps.希望能帮助到你。
Good luck!祝你好运!
function parseString(str) {
var num = [];
var nonNum = [];
for (let i of str.split("")) {
if (/\D/.test(i)) {
nonNum.push(i)
break;
} else {
num.push(i)
}
}
if (num.length !== 0) {
return [num.join(""), str.substring(num.length)]
} else {
return (num.length === 0) ? ["", str] : [num.join(""), ""]
}
}
console.log(parseString("3px"), parseString("01b"), parseString("01"), parseString("b"), parseString("1.5"), parseString("300,5"), parseString("a1"))
I feel this question points out a benefit in PHP which we do not enjoy in Javascript.我觉得这个问题指出了我们在 Javascript 中不喜欢的 PHP 好处。 I wrote a little hack for this.我为此写了一个小技巧。 I hope it helps.我希望它有帮助。
function split_non_numeric(str){
let strValue = '';
let intValue = parseInt(str, 10);
let matchPattern = str.match(new RegExp(intValue));
if(matchPattern === null){
intValue = '';
strValue = str;
}else{
if(matchPattern.index > 0){
intValue = str.substr(0, matchPattern.index + matchPattern[0].length);
strValue = str.substr(matchPattern.index + matchPattern[0].length);
}else{
intValue = str.slice(matchPattern.index, matchPattern[0].length)
strValue = str.substr(matchPattern[0].length);
}
}
return [intValue, strValue];
}
console.log(split_non_numeric('3px')); // [ '3', 'px' ]
console.log(split_non_numeric('01b')); // [ '01', 'b' ]
console.log(split_non_numeric('01')); // [ '01', '' ]
console.log(split_non_numeric('b')); // [ '', 'b' ]
console.log(split_non_numeric('1.5')); // [ '1', '.5' ]
console.log(split_non_numeric('300,5')); // [ '300', ',5' ]
console.log(split_non_numeric('a1')); // [ '', 'a1' ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.