查找并获取字符串中的数字 [英] Find and get only number in string

查看:98
本文介绍了查找并获取字符串中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决这个奇怪的情况:

Please help me solve this strange situation:

这是代码:

链接是这样的 - www.blablabla.ru#3

正则表达式如此:

var id = window.location.href.replace(/\D/, '' );
alert(id);

正则表达式是正确的 - 它必须只显示数字......但它没有显示数字: - (

The regular expression is correct - it must show only numbers ... but it's not showing numbers :-(

您能否建议我并提供一些关于如何只获取字符串数字的信息?

Can you please advice me and provide some informations on how to get only numbers in the string ?

谢谢

推荐答案

您只是用空字符串替换第一个非数字字符。请尝试使用:

You're replacing only the first non-digit character with empty string. Try using:

var id = window.location.href.replace(/\D+/g, '' ); alert(id);

(注意正则表达式末尾的全局标志)。

(Notice the "global" flag at the end of regex).

这篇关于查找并获取字符串中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆