如何使用正则表达式match()
在jQuery中将数字和字符串与特殊字符分开?
例如:
str = "+10días" // this is my the string. (number + string along with any special character)
string = str.match("needed stuf");
我需要这样string[0] = 10
和string[1] = "días"
。我怎么能这样做?
答案 0 :(得分:0)
试试这段代码:
var str = "+10días"
var parts= str.match(/[^0-9]+|[0-9]+/g)
答案 1 :(得分:0)
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var str = "+10días"
var arr = new Array();
str = str.replace(str.match(/[^\w\*]/), '')
arr.push(str.match(/^[0-9]+/g)[0])
str = str.replace(str.match(/^[0-9]+/g)[0], '')
arr.push(str)
console.log(arr)
});
</script>
</head>
</html>