Javascript / Jquery - 从字符串中获取数字 [英] Javascript / Jquery - Get number from string
本文介绍了Javascript / Jquery - 从字符串中获取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
字符串看起来像这样
"blabla blabla-5 amount-10 blabla direction-left"
如何在金额 -
之后获得该数字,和之后的文字方向 -
?
How can I get the number just after "amount-"
, and the text just after "direction-"
?
推荐答案
var s = "blabla blabla-5 amount-10 blabla direction-left";
var amount = parseInt(/amount-(\d+)/.exec(s)[1], 10);
var direction = /direction-([^\s]+)/.exec(s)[1];
如果缺少金额或方向,代码将导致错误;如果可能,请在索引到应返回的数组之前检查exec的结果是否为非null。
The code will cause an error if the amount or direction is missing; if this is possible, check if the result of exec is non-null before indexing into the array that should be returned.
这篇关于Javascript / Jquery - 从字符串中获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文