Javascript 使用java脚本或正则表达式从值中拆分数字和字符串

Javascript 使用java脚本或正则表达式从值中拆分数字和字符串,javascript,regex,string,numbers,Javascript,Regex,String,Numbers,我有一个值“4.66磅” 我想用正则表达式把“4.66”和“lb”分开 我尝试了下面的代码,但它只分隔了数字“4,66”!!但是我想要4.66和lb的值 var text = "4.66lb"; var regex = /(\d+)/g; alert(text.match(/(\d+)/g)); 尝试一下: var res = text.match(/(\d+(?:\.\d+)?)(\D+)/); res[1]包含4.66 res[2]包含lb 为了同时匹配4/5lb,您可以使用: var

我有一个值“4.66磅”

我想用正则表达式把“4.66”和“lb”分开

我尝试了下面的代码,但它只分隔了数字“4,66”!!但是我想要4.66和lb的值

var text = "4.66lb";
var regex = /(\d+)/g;
alert(text.match(/(\d+)/g));
尝试一下:

var res = text.match(/(\d+(?:\.\d+)?)(\D+)/);
res[1]
包含
4.66

res[2]
包含
lb

为了同时匹配
4/5lb
,您可以使用:

var res = text.match(/(\d+(?:[.\/]\d+)?)(\D+)/);

你也可以使用角色类

> var res = text.match(/([0-9\.]+)(\w+)/);
undefined
> res[1]
'4.66'
> res[2]
'lb'
让我举例说明
您是否尝试过添加
([a-z]+)
?简短而甜美,+1.)嘿,我还有一个场景,数字可以是4/5磅,所以在这种情况下,我需要拆分4/5磅和1磅……那么这个正则表达式是什么呢?@user3770003:用斜杠替换点:
/(\d+(?:\/\d+)(\d+)/
var str = ' 1 ab 2 bc 4 dd';   //sample string

str.split(/\s+\d+\s+/)
result_1 = ["", "ab", "bc", "dd"]  //regex not enclosed in parenthesis () will split string on the basis of match expression

str.split(/(\s+\d+\s+)/)        //regex enclosed in parenthesis () along with above results, it also finds all matching strings
result_2 = ["", " 1 ", "ab", " 2 ", "bc", " 4 ", "dd"] 

//here we received two type of results: result_1 (split of string based on regex) and those matching the regex itself

//Yours case is the second one
//enclose the desired regex in parenthesis
solution : str.split(/(\d+\.*\d+[^\D])/)