简体   繁体   English

如何使用js获取字符串中的整数值

[英]how to get integer values in a string with js

var text = "rating-1-gray-3-blue";

i want to get value 13 我想获得价值13

how can i do this . 我怎样才能做到这一点 。 basically i want to get all integers inside a string . 基本上我想在字符串中获取所有整数。

i tried with parseInt , but it returns int when the integer value is at the beginning of the string 我尝试使用parseInt ,但是当整数值在字符串的开头时,它返回int

thanks in advance 提前致谢

您可以使用正则表达式

var result = +text.replace(/\D/g, "");  // 13
var result=text.replace(/[^0-9]/g, ''); 

这将从您的字符串中删除所有非0-9值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM