[英]split javascript string using a regexp to separate numbers from other characters
so for example:例如:
"10.cm"
...becomes... [10,".cm"]
or ["10",".cm"]
, either will do as I can work with a string once it's split up. "10.cm"
...变成... [10,".cm"]
或["10",".cm"]
,一旦它被分开,我可以使用字符串。
i tried我试过了
"10.cm".split(/[0-9]/|/[abc]/)
but it seems that i don't have such a great understanding of using regexp's但似乎我对使用正则表达式没有很好的理解
thanks谢谢
You may tokenize the string into digits and non-digits with /\\d+|\\D+/g
regex: 您可以使用/\\d+|\\D+/g
正则表达式将字符串标记为数字和非数字:
var s = "10.cm"; console.log(s.match(/\\d+|\\D+/g));
Details : 详细资料 :
\\d+
- matches 1 or more digits \\d+
-匹配1个或多个数字 |
- or - 要么 \\D+
- matches 1 or more characters other than digits. \\D+
-匹配数字以外的1个或多个字符。 /\W/ Matches any non-word character. /\W/匹配任何非单词字符。 This includes spaces and punctuation, but not underscores.这包括空格和标点符号,但不包括下划线。 In this solution can be used /\W/ with split and join methods.在此解决方案中,可以将/\W/与拆分和连接方法一起使用。 You can separate numbers from other characters.您可以将数字与其他字符分开。
let s = "10.cm";
console.log(s.split(/\W/).join(" "));
output = 10 cm output = 10 厘米
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.