简体   繁体   English

使用javascript提取并粘贴一部分字符串

[英]extracting and pasting a part of string using javascript

I have people inputting 125px and 125% I want to extract just 125 from this perform some calulation on it. 我要输入125px和125%的人,我想从中提取125px,然后对它进行一些计算。 125 * 2 Then I want to add px or % as it was set before 250px and 250% 125 * 2然后,我想添加250px和250%之前设置的px或%

I'm guessing something like this should work: 我猜像这样的事情应该工作:

//str is equal to your string
var num = str.substring(0,str.search(/[^0-9]/))/1;
var sub = str.substring(str.search(/[^0-9]/));
num=num*2;
var newstr = num+sub;

Emile uses parseFloat to get the number and replace to get rid of anything that isn't the unit: Emile使用parseFloat来获取数字并replace以摆脱不是单位的任何东西:

function parse(prop){
    var p = parseFloat(prop),
        q = prop.replace(/^[\-\d\.]+/, '');

    ...
}

Then p is the number, and q is the unit. p是数字, q是单位。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM