[英]Javascript split once at first occurrence of a number
我有一个变量,在第一次出现导致字母和数字成分的数字时,需要将其拆分一次。
情况1
var str = "S44.5";
I need the output to be "S" and "44.5"
情况#2
var str = "44.5";
I need the output to be "" and "44.5"
如何使用Javascript执行此操作? 请帮忙!
简
这会在数字前拆分一个字母,如果不发生这种情况,它还会包含一个空字符串。
var a = /([az]*)([\\w.]+)/i.exec("S44.5"); var b = /([az]*)([\\w.]+)/i.exec("44.5"); console.log("I need the output to be %s and %s", JSON.stringify(a[1]), JSON.stringify(a[2])) console.log("I need the output to be %s and %s", JSON.stringify(b[1]), JSON.stringify(b[2]))
使用正则表达式
var text= "hello12";
var text= "s4.44";
var text= "4.53";
var text= "aaa4.53";
var matches = text.match(/([a-zA-Z]*)([0-9\.]+)/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.