拆分带有十进制数字的字符串和一些字符[复制]

时间:2023-02-14 18:13:22

This question already has an answer here:

这个问题在这里已有答案:

I have strings like: '1234picas' '1234 px' '145.4us'

我有一些字符串:'1234picas''1234 px''145.4us'

I want to split them in two parts: the numeric part and the non numeric one. For example: '1234.4us' need to be splitted in '1234.4' and 'us'. I am tempted to insert a separator between the last digit and the non numeric and use split but is there a better way to do this in JavaScript

我想将它们分成两部分:数字部分和非数字部分。例如:'1234.4us'需要在'1234.4'和'us'中拆分。我很想在最后一个数字和非数字之间插入一个分隔符并使用split,但是有更好的方法在JavaScript中执行此操作

Thanks

谢谢

Note: this is not the slitting of a string at special char. it could be converted to that but this is what I am trying to avoid.

注意:这不是特殊字符串中的字符串分割。它可以转换为,但这是我想避免的。

6 个解决方案

#1


1  

You can do it using String.prototype.match():

你可以使用String.prototype.match()来做到这一点:

const a = '1234picas';
const b = '1234 px';
const c = '145.4us';

function split(input) {
  const splitArray = input.match(/([\d\.]+)(.*)/); // match only digits and decimal points in the first group and match the rest of the string in second group
  
  return {
    numeric: splitArray[1],
    nonnumeric: splitArray[2],
  };
}

console.log(split(a));
console.log(split(b));
console.log(split(c));

Regex explanation | Regex101

正则表达式的解释| Regex101

#2


1  

You can use .split with a regex using a lookahead:

您可以使用前瞻使用.split与正则表达式:

str.split(/(?=[^\d.-])/g))
   .map(y => [
         y[0],
         y.slice(1).join('').trim()
   ])

x = ["1234picas", "1234 px", "145.4us"];

console.log(x.map(y => 
    y.split(/(?=[^\d.-])/g))
     .map(y => [
         y[0],
         y.slice(1).join('').trim()
     ])
)

#3


1  

You can do like this in javascript to split:

您可以在javascript中执行此操作以进行拆分:

var myString = '145.4us';
var splits = myString.split(/(\d+\.?\d+)/);
console.log(splits);

#4


1  

here's one way to do it using parseFloat() and slice() , you can add it to the Sting.prototype if you want :

这是使用parseFloat()和slice()执行此操作的一种方法,如果需要,可以将其添加到Sting.prototype:

const str1 = '1234picas',
  str2 = '1234 px',
  str3 = '145.4us';

String.prototype.split = function () {
  const num = parseFloat(this);
  const alph = this.slice(num.toString().length, this.length)

  return {
    num,
    alph
  }
}


console.log(str1.split());
console.log(str2.split());
console.log(str3.split());

#5


1  

This may help you:

这可能对您有所帮助:

var a = "4343.453fsdakfjdsa";
a.match(/[a-zA-Z]+|[\d\.?]+/ig);

MDN for String.match

String.match的MDN

It basically says match the alphabetical letters OR match numbers with an optional period. The ig is insensitive (which is not really needed) and global as in don't return on the first match, keep going until all of the string has been parsed.

它基本上表示匹配字母字母或匹配数字与可选的句点。 ig是不敏感的(这不是真正需要的)和全局的,因为在第一次匹配时不返回,继续进行直到所有字符串都被解析。

#6


1  

Regex!
Here is how it works: https://regex101.com/r/XbI7Mq/1

正则表达式!以下是它的工作原理:https://regex101.com/r/XbI7Mq/1

const test = ['1234picas', '1234 px', '145.4us', 'no'];
const regex = /^(\d+\.?\d+)\s?(.*)/;
test.forEach(i => {
  const result = regex.exec(i);
  if (result) {
    console.log(result[1], result[2])
  }
});

#1


1  

You can do it using String.prototype.match():

你可以使用String.prototype.match()来做到这一点:

const a = '1234picas';
const b = '1234 px';
const c = '145.4us';

function split(input) {
  const splitArray = input.match(/([\d\.]+)(.*)/); // match only digits and decimal points in the first group and match the rest of the string in second group
  
  return {
    numeric: splitArray[1],
    nonnumeric: splitArray[2],
  };
}

console.log(split(a));
console.log(split(b));
console.log(split(c));

Regex explanation | Regex101

正则表达式的解释| Regex101

#2


1  

You can use .split with a regex using a lookahead:

您可以使用前瞻使用.split与正则表达式:

str.split(/(?=[^\d.-])/g))
   .map(y => [
         y[0],
         y.slice(1).join('').trim()
   ])

x = ["1234picas", "1234 px", "145.4us"];

console.log(x.map(y => 
    y.split(/(?=[^\d.-])/g))
     .map(y => [
         y[0],
         y.slice(1).join('').trim()
     ])
)

#3


1  

You can do like this in javascript to split:

您可以在javascript中执行此操作以进行拆分:

var myString = '145.4us';
var splits = myString.split(/(\d+\.?\d+)/);
console.log(splits);

#4


1  

here's one way to do it using parseFloat() and slice() , you can add it to the Sting.prototype if you want :

这是使用parseFloat()和slice()执行此操作的一种方法,如果需要,可以将其添加到Sting.prototype:

const str1 = '1234picas',
  str2 = '1234 px',
  str3 = '145.4us';

String.prototype.split = function () {
  const num = parseFloat(this);
  const alph = this.slice(num.toString().length, this.length)

  return {
    num,
    alph
  }
}


console.log(str1.split());
console.log(str2.split());
console.log(str3.split());

#5


1  

This may help you:

这可能对您有所帮助:

var a = "4343.453fsdakfjdsa";
a.match(/[a-zA-Z]+|[\d\.?]+/ig);

MDN for String.match

String.match的MDN

It basically says match the alphabetical letters OR match numbers with an optional period. The ig is insensitive (which is not really needed) and global as in don't return on the first match, keep going until all of the string has been parsed.

它基本上表示匹配字母字母或匹配数字与可选的句点。 ig是不敏感的(这不是真正需要的)和全局的,因为在第一次匹配时不返回,继续进行直到所有字符串都被解析。

#6


1  

Regex!
Here is how it works: https://regex101.com/r/XbI7Mq/1

正则表达式!以下是它的工作原理:https://regex101.com/r/XbI7Mq/1

const test = ['1234picas', '1234 px', '145.4us', 'no'];
const regex = /^(\d+\.?\d+)\s?(.*)/;
test.forEach(i => {
  const result = regex.exec(i);
  if (result) {
    console.log(result[1], result[2])
  }
});